0

I want to Sort a Linked List, I have watched so many tutorials videos and examples, and I know how to sort a Linked List, but only Linked Lists with a single String, my Linked List is composed by sentences and I have to sort using one word in the sentences for example.

this list (Number Two: 2), (Number three : 3), (Number one : 1) like this (Number one: 1), (Number Two: 2), (Number three : 3)

I tried to split my linked list and use an iterator but I don't know what to do after.please help.

private LinkedList<String> list = new LinkedList<String>();

list.add("Number three : 3");
list.add("Number One : 1");
list.add("Number two : 2");

for(Iterator<String> iterator =list.iterator(); iterator.hasNext(); ) 
                {
                    String string = iterator.next();
                   for (String word : string.split(" ")){

                     /* if (int.TryParse(word)){ Maybe to get the number?


                      }*/

                   }


            }

15 hours trying to figure out this, so any idea is welcome :).

1
  • why don't you use Collections.sort(list); Commented Sep 16, 2014 at 3:52

2 Answers 2

3

There are probably a few ways to this, but you could write a custom Comparator which calculates the difference between the two values passed to it in the manner you want...

LinkedList<String> list = new LinkedList<String>();

list.add("Number three : 3");
list.add("Number three : 1");
list.add("Number three : 2");

System.out.println("Not sorted...");
for (String value : list) {
    System.out.println(value);
}

Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        String w1 = getWordFrom(o1);
        String w2 = getWordFrom(o2);
        return w1.compareTo(w2);
    }

    protected String getWordFrom(String value) {

        String[] parts = value.split(":");
        return parts[1].trim();

    }
});

System.out.println("Now sorted...");
for (String value : list) {
    System.out.println(value);
}

Will output

Not sorted...
Number three : 3
Number three : 1
Number three : 2
Now sorted...
Number three : 1
Number three : 2
Number three : 3

Now, this is a very simple example and doesn't have a condition for a String missing the delimiter, but I guess you get the idea...

Sign up to request clarification or add additional context in comments.

Comments

0

Try this code :

public class SortList {
    private static LinkedList<String> list= new LinkedList<String>();
    public static void main(String s[]) {

        list.add("Number three : 3");
        list.add("Number three : 1");
        list.add("Number three : 2");

        System.out.println("list :"+list);
        Collections.sort(list);
        System.out.println("sorted list :"+list);
    }
}

1 Comment

That only works if all the elements are the same except for the number. sorry my list.add codes were wrong I edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.