alpha 对具有相同整数值且应按升序排列的列表进行排序[重复]

Posted

技术标签:

【中文标题】alpha 对具有相同整数值且应按升序排列的列表进行排序[重复]【英文标题】:alpha sort the list which has same integer values and should be in ascending order [duplicate] 【发布时间】: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 标记? 【参考方案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,
  );


【讨论】:

以上是关于alpha 对具有相同整数值且应按升序排列的列表进行排序[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Haskell - 按整数值对带有字符串的列表进行排序的函数

按第二项(整数值)对元组列表进行排序[重复]

Java按整数字段对对象数组进行排序,如果它们相同,则按另一个整数值排序

在组合框中选择升序时如何对元素列表进行排序

如何按升序对这个浮点数值列表进行排序?

javascript while循环正确迭代但具有相同逻辑的for循环不是,在具有整数值和一些空值的数组上