简体   繁体   English

如何对LinkedList <String>进行排序?

[英]How to sort LinkedList<String>?

I need to sort the Strings of a LinkedList by the length of the Strings, but would like to keep the order of same-length strings (not sorted lexicographically). 我需要按字符串的长度对LinkedList的字符串进行排序,但是希望保持相同长度字符串的顺序(不按字典顺序排序)。

Sample input: 样本输入:

this
is
just
a
test

Sample Output: 样本输出:

a
is
this
just
test

I am trying to do this with a Comparable<LinkedList<String>> and a compareTo method, but I don't get the correct output (mine still sorts it lexicographically) 我试图用Comparable<LinkedList<String>>compareTo方法做到这一点,但我没有得到正确的输出(我仍然按字典顺序对其进行排序)

public class Q3_sorting implements Comparable<LinkedList<String>> {
    Scanner keyboardScanner = null;
    LinkedList<String> fileList = new LinkedList<String>();

// [...] some code here // [...]这里有一些代码

public int compareTo(LinkedList<String> o) {
        // TODO Auto-generated method stub
        o = fileList;

        for (int i = 0; i < fileList.size() -1; i++) {
            if (fileList.get(i).length() == o.get(i+1).length()) {
                return 0;
            }
            if (fileList.get(i).length() > o.get(i+1).length()) {
                return -1;
            }
            if (fileList.get(i).length() < o.get(i+1).length()) {
                return 1;
            }

        }

I then use 然后我用
Q3_sorting sort = new Q3_sorting(args);
Collections.sort(sort.fileList); in my main method. 在我的主要方法。 I then print the list out... 然后我打印出清单......

but I get this as output: 但我得到这个作为输出:

a
is
just
test
this

How would I rectify this problem? 我该如何纠正这个问题?

You should create a comparator: 你应该创建一个比较器:

public class Q3_sorting implements Comparator<String> {
public int compare(String a, String b) {
 return a.length() - b.length();
}

And then sort it with the method: 然后使用以下方法对其进行排序:

Collections.sort(list, new Q3_sorting());

Note that what you want to do is sort the Strings inside the List. 请注意,您要做的是对List中的字符串进行排序。 By implementing a comparator of List (or a comparable, as it works on the same purpose here) what you are telling the JVM is that you want to compare different List's. 通过实现List的比较器(或类似的,因为它在这里工作的目的相同),你告诉JVM的是你要比较不同的List。

You could also achieve your objective by implementing a Comparable in the class to sort, but you can't as long as String is final so you cannot extend. 你也可以通过在类中实现Comparable来实现你的目标,但是你不能只要String是最终的,所以你不能扩展。 Therefore there is no other way than to implement a Comparator, which is simpler too :) 因此除了实现比较器之外别无他法,这也比较简单:)

Use the Collections.sort(list, comparator) overload. 使用Collections.sort(list, comparator)重载。 You need a Comparator<String> not a Comparator<LinkedList<String>> . 您需要Comparator<String>而不是Comparator<LinkedList<String>> Note that the javadoc of Collections.sort guarantees a stable sort (keep the order of equal strings, equal mean equal according you comparator). 请注意,Collections.sort的javadoc保证了一个稳定的排序(保持相等字符串的顺序,根据比较器等于平均值​​)。

You are sorting strings, not lists of strings. 您正在排序字符串,而不是字符串列表。 To do this, you need to define a Comparator<String> to compare two strings by their length as follows: 为此,您需要定义Comparator<String>以按长度比较两个字符串,如下所示:

public class ByLength implements Comparator<String> {
  @Override
  public int compare(String a, String b) {
    return a.length() - b.length();
  }
}

Then, to sort the list, you need to call: 然后,要对列表进行排序,您需要调用:

Collections.sort(sort.fileList, new ByLength());

Also note that sorting a LinkedList is very inefficient and you should use an ArrayList instead. 另请注意,对LinkedList进行排序效率非常低,您应该使用ArrayList

Strings unfortunately don't have a property that signifies what position they hold in a linked list. 遗憾的是,字符串没有表示它们在链表中保持什么位置的属性。 You will therefore need to create a custom data object that does keep track of that information. 因此,您需要创建一个跟踪该信息的自定义数据对象。 Either that or write your own custom sort method for the linked list and call that instead of Collections.sort(). 要么是为链表创建自己的自定义排序方法,要么调用它而不是Collections.sort()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM