【问题标题】:Sorting an ArrayList of String[] using multiple Indices使用多个索引对 String[] 的 ArrayList 进行排序
【发布时间】:2016-02-12 11:06:41
【问题描述】:

虽然我已经使用这个网站一段时间了,但我还是刚开始在这里积极写问题。

我想根据数组的前两个索引按词法顺序(= 自然顺序?!)对 ArrayList 进行排序。目前我使用了以下代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class SortArrayList {

    public static void main(String[] args) {

        ArrayList<String[]> workingSet = new ArrayList<>();
        workingSet.add(new String[]{"MiningCorp", "2265 Betacity"});
        workingSet.add(new String[]{"MiningCorp", "6454 Iotacity"});
        workingSet.add(new String[]{"Arbiter", "3812 Gammacity"});
        workingSet.add(new String[]{"MiningCorp", "1234 Thetacity"});
        workingSet.add(new String[]{"Arbiter", "1812 Deltacity"});

        Comparator<String[]> staComp = new Comparator<String[]>() {

            @Override
            public int compare(String[] first, String[] second) {
                String composite1 = first[0] + " " + first[1];
                String composite2 = second[0] + " " + second[1];
                return composite1.compareTo(composite2);
            }
        };

        Collections.sort(workingSet, staComp);
        for(String[] arr : workingSet){
            System.out.println(Arrays.toString(arr));
        }


    }

}

这应该会产生以下输出:

[Arbiter, 1812 Deltacity]
[Arbiter, 3812 Gammacity]
[MiningCorp, 1234 Thetacity]
[MiningCorp, 2265 Betacity]
[MiningCorp, 6454 Iotacity]

这正是我想要的。 有没有更优雅的方式使用预建方法?

如果我想通过按词法顺序按第一个数组条目分组来排序,但在这个组中我希望各个数组按逆词法顺序排序,该怎么办? 为此,我是否需要第二个比较器来首先对每个数组的第二个索引的条目进行预排序?

这是我想为这个例子得到的:

[Arbiter, 3812 Gammacity]
[Arbiter, 1812 Deltacity]
[MiningCorp, 6454 Iotacity]
[MiningCorp, 2265 Betacity]
[MiningCorp, 1234 Thetacity]

【问题讨论】:

    标签: java arrays sorting arraylist


    【解决方案1】:

    如果 java8 是一个选项,那么我会使用 thenComparing 方法来组合两个排序说明符。

    当使用reversed 时,比较器的结果会颠倒。

    示例代码:

    // turn your list into a stream
    workingSet.stream()
    
    // sort it...
    .sorted(
    
        // first sort specifier: 0th element of Array
        Comparator.<String[], String>comparing(composite -> composite[0])
    
            // combine sort specifiers
            .thenComparing(
    
                    // second sort specifier: 2st element of Array
                    Comparator.<String[], String>comparing(composite -> composite[1])
    
                    // REVERSED!
                    .reversed()
            )
        )
    
    // convert each array to a String
    .map(Arrays::toString)
    
    // print each String
    .forEach(System.out::println);
    

    【讨论】:

      【解决方案2】:

      如果我想通过按词汇顺序的第一个数组条目进行分组来进行排序,但在这个组中我希望各个数组按逆词汇顺序排序?

      那么你需要以不同的方式实现Comparator

      Comparator<String[]> secondComparator = new Comparator<String[]>() {
              @Override
              public int compare(String[] first, String[] second) {
                  int compareFirstPart = first[0].compareTo(second[0]);
                  if(compareFirstPart != 0)
                      return compareFirstPart;
                  else
                      return second[1].compareTo(first[1]); // Inverse!
              }
          };
      

      【讨论】:

        【解决方案3】:

        如果你使用 java 8,你也可以使用 lambda 表达式:

        Collections.sort(workingSet, (first, second) -> {
                    int compareFirstPart = first[0].compareTo(second[0]);
                    if(compareFirstPart != 0)
                        return compareFirstPart;
                    else
                        return second[1].compareTo(first[1]); 
        });
        

        【讨论】:

        • 虽然 lambda 表达式对我来说并不是全新的,但这让我更清楚它们是如何在 java 中工作的。谢谢你的回答。
        猜你喜欢
        • 1970-01-01
        • 2011-03-11
        • 2016-08-28
        • 2012-03-24
        • 2013-11-29
        • 2011-11-06
        • 2012-11-27
        • 2019-09-26
        • 1970-01-01
        相关资源
        最近更新 更多