简体   繁体   中英

method to sort a linked list in java

okay so i have this issue where all my info are being sent to the linked list as objects, which is what i want. the linked lists are initalized from another class, this other class contains all the methods for the LinkedLists. is there a method to sort the objects in the linked list based on numbers? because each element looks something like this ("800110 Jane Hey are you there"). before going in, they are set as an int, string, string:

        //if the command is to RecieveMessage
        SinglyLinkedList <String> Jane= new SinglyLinkedList <String>();
        int Time;
        String Name;
        String Message
        Time=in.nextInt();
        Name=" "+in.next();
        Message=in.nextLine();
        if(Name.equals(" Jane")) {
            //sends message to Janeand increases the number of 
            //unread messages and notifies Jane
        Jane.addLast(Time+Name+Message);

the ints arent in order, but i have to order them and i dont know how and collections sort isnt allowed. may you please help me? the .addlast come from the standard Linked List function. i have to sort 4 other users the same way, so if theres one method to sort all of them by time stamp, that would be amazing. im also using this method to extract the time:

    public static int extractTime(String Sentence) {
        int length = Sentence.length();
        String result = "";
        for (int i = 0; i < length; i++) {
            Character character = Sentence.charAt(i);
            if (Character.isDigit(character)) {
                result += character;
            }
        }
        return Integer.parseInt(result);
}

the String sentence being the Element at a specific node using the for loop:

    for(int i=0; i < Jane.size();i++) {
                UnreadMessage=Jane.first().toString();
                int returnTime= extractTime(UnreadMessage);
                }

please help, this will help me so much.

Write an inner class TimeComp which implements the comparator<> interface. There we will specify the strategy to sort the Person objects and pass it to Collections.sort() method:

class Person{

   int time;
   String name;
   String message;
   //Write get and set methods for all member variables

   class TimeComp implements Comparator<Person>{
       @Override
       public int compare(Person p1, Person p2) {
        if(p1.getTime() < p2.getTime()){
            return 1;
        } else {
            return -1;
        }
    }
   } 
}

List<Person> persons = new LinkedList<Person>();
Collections.sort(persons, TimeComp);

If you're getting the nodes to insert into the linked list one at a time, you could use a variation of bottom up merge sort, which uses a small array (25 to 32) of linked lists to hold sorted lists, where array[i] is either null or is a reference to a linked list with 2^i (2 to the power i) nodes. Each node is merged in to the array one at at time and merge operations are used to update the array. Once the last node is added, then the array is merged to form a single sorted list. Wiki has a psuedo code example:

https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation_using_lists

Using bottom up merge sort for this case will have time complexity O(n log(n)), versus O(n^2) for an insertion sort like method.

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