Dart列表应该根据元素对象内相同的整数值按字母顺序排序。如果整数具有相同的值,则相关字符串应按顺序排列和升序。
这是名单。
List<People> items = [
People( 10 , 'a' ) ,
People( 5 , 'c' ),
People( 15 , 'b' ),
People( 15 , 'a' ),
People( 5 , 'k' ),
People( 10 , 'd' )
People( 7, 'c' )];预期结果:
List<People> items = [
People( 5 , 'c' ) ,
People( 5 , 'k' ),
People( 7 , 'c' ),
People( 10 , 'a' ),
People( 10 , 'k' ),
People( 15 , 'a' )
People( 15, 'd' )];发布于 2021-12-23 07:42:43
看起来,所提供的输入的预期输出应该是:
[People(5, 'c', People(5, 'k', People(7, 'c', People(10, 'a', People(10, 'd', People(15, 'a', People(15, 'b']尝试下面的代码以获得所需的输出:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
ArrayList<People> people = new ArrayList<>();
people.add(new People(10, 'a'));
people.add(new People(5, 'c'));
people.add(new People(15, 'b'));
people.add(new People(15, 'a'));
people.add(new People(5, 'k'));
people.add(new People(10, 'd'));
people.add(new People(7, 'c'));
System.out.println(people);
Collections.sort(people, Comparator.comparing(People::getNumber)
.thenComparing(People::getaChar));
System.out.println(people);
}
}
class People {
private int number;
private char aChar;
public People(int number, char aChar) {
this.number = number;
this.aChar = aChar;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public char getaChar() {
return aChar;
}
public void setaChar(char aChar) {
this.aChar = aChar;
}
@Override
public String toString() {
return "People(" + number +
", '" + aChar +
"'";
}
}发布于 2021-12-23 07:46:40
在dart语言中,您可以像下面的代码示例那样对其进行排序。
void main() {
final items = [ People( 10 , 'a' ) , People( 5 , 'c' ), People( 15 , 'b' ), People( 15 , 'a' ), People( 5 , 'k' ), People( 10 , 'd' ), People( 7, 'c' )];
items.sort((a,b)=> a.string.compareTo(b.string));
items.forEach((item)=> print(item.string + item.value.toString()));
}
class People{
final int value;
final String string;
People(this.value, this.string);
}产出;
a10
a15
b15
c5
c7
d10
k5发布于 2021-12-23 08:13:45
下面是排序列表的完整示例
主
void main() {
//List of items
List<People> items = [
People(10, 'a'),
People(5, 'k'),
People(5, 'c'),
People(15, 'b'),
People(15, 'a'),
People(10, 'd'),
People(7, 'c')
];
//Printing List before sorting
Methods.printList(items, "\n\nBefore\n\n");
//Step 01 : Sort the List according to alpha order
items.sort((a, b) => a.string.compareTo(b.string));
//Step 02 : Sort the list according to num order
items.sort(Methods.sortMyListNumOrder);
//Printing List after sorting
Methods.printList(items, "\n\nAfter\n\n");
}方法类
//class of Methods i.e printing and sorting
class Methods{
static void printList(List<People> items, String value){
print(value);
for(var item in items){
print("People(${item.integer} , ${item.string})");
}
}
static int sortMyListNumOrder(People first, People second) {
final int firstValue = first.integer;
final int secondValue = second.integer;
if (firstValue < secondValue) {
return -1;
} else if (firstValue > secondValue) {
return 1;
} else {
return 0;
}
}
}People级
//People class
class People {
final String string;
final int integer;
People(
this.integer,
this.string,
);
}https://stackoverflow.com/questions/70458967
复制相似问题