【问题标题】:alpha sort the list which has same integer values and should be in ascending order [duplicate]alpha 对具有相同整数值且应按升序排列的列表进行排序[重复]
【发布时间】:2022-01-24 06:19:00
【问题描述】:

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

这是列表。

 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' )];

【问题讨论】:

  • 如果这是一个 Dart 问题,为什么要使用 Java 标记?

标签: flutter sorting dart


【解决方案1】:

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

[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 +
                "'";
    }
}

【讨论】:

    【解决方案2】:

    在 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
    

    【讨论】:

    • 我需要排序,如果整数相同,则相关字符串应按字母顺序排列,整数值应按升序排列
    【解决方案3】:

    这是排序列表的完整示例

    主要

    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 class
    class People {
      final String string;
      final int integer;
    
      People(
        this.integer,
        this.string,
      );
    }
    
    

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多