简体   繁体   中英

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).

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)

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. 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.

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. Therefore there is no other way than to implement a Comparator, which is simpler too :)

Use the Collections.sort(list, comparator) overload. You need a Comparator<String> not a 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).

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:

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.

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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