簡體   English   中英

對包含字符串和整數的 2D 列表進行排序,按整數 asc 排序我該怎么做? (JAVA)

[英]Sorting a 2D List containing both Strings and Integers, sort by integers asc how do i do? (JAVA)

          theList.add(new 2DList("abc123", 9986));
          theList.add(new 2DList("ads314", 1234));
          theList.add(new 2DList("dal192", 3214));
          theList.add(new 2DList("sam273", 9823));
          theList.add(new 2DList("wor213", 7643));
          theList.add(new 2DList("wos987", 1292));
          theList.add(new 2DList("cat202", 9382));
          theList.add(new 2DList("jga213", 9432));
          theList.add(new 2DList("gog113", 1493));
          theList.add(new 2DList("aba231", 9831));

我想按從低到高對整數進行排序,這樣做還對字符串進行排序並將排序后的數組保存在一個新數組中。

也許通過使用收集器 java 比較? 但我真的不知道怎么做,我想學習。

List<2DList> sortedList = theList.stream().sorted(Comparator.comparing(2DList::getValue)).collect(Collectors.toList());

如果您想在單獨的列表中獲取值:

List<Integer> valueList = theList.stream().map(v -> v.getValue()).sorted().collect(Collectors.toList());
List<String> nameList = theList.stream().map(v -> v.getName()).sorted().collect(Collectors.toList());

我會在數字上使用穩定的排序算法,然后在字符串上使用。 這是示例代碼。

import java.util.*;
class DList{
    DList(String n, int v){ name =n; value=v;}
    String name;
    int value;
    public String toString(){
        return "DList[" + name + ", " + value + "]";
    }
}
public class Main{
    public static void main(String[] args) {
        List<DList> theList = new ArrayList<>();
          theList.add(new DList("abc123", 9986));
          theList.add(new DList("ads314", 1234));
          theList.add(new DList("dal192", 3214));
          theList.add(new DList("sam273", 9823));
          theList.add(new DList("wor213", 7643));
          theList.add(new DList("wos987", 1292));
          theList.add(new DList("cat202", 9382));
          theList.add(new DList("jga213", 9432));
          theList.add(new DList("gog113", 1493));
          theList.add(new DList("aba231", 9831));
          
          theList.forEach(System.out::println);
          System.out.println("==============");
          theList.sort((e1,e2)->{return e1.value-e2.value;});
          theList.sort((e1,e2)->{return e1.name.compareTo(e2.name);});
          theList.forEach(System.out::println);
    }
}

Output:

DList[ads314, 1234]
DList[wos987, 1292]
DList[gog113, 1493]
DList[dal192, 3214]
DList[wor213, 7643]
DList[cat202, 9382]
DList[jga213, 9432]
DList[sam273, 9823]
DList[aba231, 9831]
DList[abc123, 9986]
==============
DList[aba231, 9831]
DList[abc123, 9986]
DList[ads314, 1234]
DList[cat202, 9382]
DList[dal192, 3214]
DList[gog113, 1493]
DList[jga213, 9432]
DList[sam273, 9823]
DList[wor213, 7643]
DList[wos987, 1292]

首先,分配一條記錄來保存字符串和整數。 記錄是不可變的 class。 也可以使用 class。

record Data(String getString, int getValue) {
    @Override
    public String toString() {
        return String.format("[%s, %s]", getString, getValue);
    }
}

現在創建一個數據記錄列表。

List<Data> theList = new ArrayList<>(List.of(new Data("abc123", 9986),
        new Data("ads314", 1234), new Data("dal192", 3214),
        new Data("sam273", 9823), new Data("wor213", 7643),
        new Data("wos987", 1292), new Data("cat202", 9382),
        new Data("jga213", 9432), new Data("gog113", 1493),
        new Data("aba231", 9831)));

並開始排序過程。

  • 對原始列表進行排序
  • byString添加到新列表
  • 打印排序后的字符串。
  • 重復存儲在byValue中的整數的過程
theList.sort(Comparator.comparing(Data::getString));
List<Data> byString = new ArrayList<>(theList);
System.out.println("Sorted by String\n");
byString.forEach(System.out::println);

theList.sort(Comparator.comparing(Data::getValue));
List<Data> byValue = new ArrayList<>(theList);
System.out.println("\nSorted by value\n");
byValue.forEach(System.out::println);

以上印刷品

Sorted by String

[aba231, 9831]
[abc123, 9986]
[ads314, 1234]
[cat202, 9382]
[dal192, 3214]
[gog113, 1493]
[jga213, 9432]
[sam273, 9823]
[wor213, 7643]
[wos987, 1292]

Sorted by value

[ads314, 1234]
[wos987, 1292]
[gog113, 1493]
[dal192, 3214]
[wor213, 7643]
[cat202, 9382]
[jga213, 9432]
[sam273, 9823]
[aba231, 9831]
[abc123, 9986]

這是另一個示例,首先按字符串排序,如果字符串相等,則按 int 排序。

List<Data> newList = new ArrayList<>(
        List.of(new Data("to", 10), new Data("be", 8),
                new Data("or", 12), new Data("not ", 15),
                new Data("to", 9), new Data("be", 4),
                new Data("that", 8), new Data("is", 2),
                new Data("the", 3), new Data("question", 2)));

newList.sort(Comparator.comparing(Data::getString)
        .thenComparing(Data::getValue));

System.out.println("\nSorting by String, then int.\n");
newList.forEach(System.out::println);

印刷

Sorting by String, then int.

[be, 4]
[be, 8]
[is, 2]
[not , 15]
[or, 12]
[question, 2]
[that, 8]
[the, 3]
[to, 9]
[to, 10]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM