首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对具有相同整数值并应按升序排列的列表进行排序

对具有相同整数值并应按升序排列的列表进行排序
EN

Stack Overflow用户
提问于 2021-12-23 07:30:58
回答 3查看 143关注 0票数 0

Dart列表应该根据元素对象内相同的整数值按字母顺序排序。如果整数具有相同的值,则相关字符串应按顺序排列和升序。

这是名单。

代码语言:javascript
复制
 List<People> items = [
  People( 10 ,  'a' ) ,
  People(  5 ,  'c' ),
  People( 15 ,  'b' ),
  People(  15 ,  'a' ),
  People(  5 , 'k' ),
  People(  10 , 'd' ) 
  People(   7, 'c' )];

预期结果:

代码语言:javascript
复制
 List<People> items = [
  People( 5 ,  'c' ) ,
  People(  5 ,  'k' ),
  People( 7 ,  'c' ),
  People(  10 ,  'a' ),
  People(  10 , 'k' ),
  People(  15 , 'a' ) 
  People(   15, 'd' )];
EN

回答 3

Stack Overflow用户

发布于 2021-12-23 07:42:43

看起来,所提供的输入的预期输出应该是:

代码语言:javascript
复制
[People(5, 'c', People(5, 'k', People(7, 'c', People(10, 'a', People(10, 'd', People(15, 'a', People(15, 'b']

尝试下面的代码以获得所需的输出:

代码语言:javascript
复制
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 +
                "'";
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-12-23 07:46:40

在dart语言中,您可以像下面的代码示例那样对其进行排序。

代码语言:javascript
复制
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);
}

产出;

代码语言:javascript
复制
a10
a15
b15
c5
c7
d10
k5
票数 0
EN

Stack Overflow用户

发布于 2021-12-23 08:13:45

下面是排序列表的完整示例

代码语言:javascript
复制
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");
}

方法类

代码语言:javascript
复制
//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级

代码语言:javascript
复制
//People class
class People {
  final String string;
  final int integer;

  People(
    this.integer,
    this.string,
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70458967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档