-1

Searching for a solution to my problem, I have found many fixes for something similar, but not identical. However, since I would categorize myself as a javascript beginner, I can't figure out how to use those fixes for my own solution. Hence, I am asking you.

I am working on a chat. On one page there is a list of conversations. Each conversation look like this: <li id="+conversationID+" class=\"people-item\"><a href=\"#\" id="+obj.newest_message_time+" class=\"list-group-item\"><span class="+conversationID+"><p class=\"participants\">" + participantsStringified + "</p><p class=\"new_message\">Me: "+obj.newest_message+"</p></span></a></li>;

As you can see, the <a> inside <li> has an ID of obj.newest_message_time, which is a number that is programmatically added when the conversations is created.

So imaginging there are multiple <li>'s, how can I sort them by using the id?

JonasSH
  • 206
  • 3
  • 17

1 Answers1

1

I have put a solution in the link below

https://jsfiddle.net/dwetouLf/

you can go ahead and change the value of the ID of anchor within LI to see if that works for you.

I am using the Jquery library foe this.

(function( $ ) {
$(function() {
    $( "li", "#test" ).sort(function( a, b ) {
        return $( a ).find('a').attr('id') > $(b).find('a').attr('id'); 
    }).appendTo( "#test" );
});

})( jQuery );
Ankit
  • 257
  • 1
  • 3
  • 13