Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to sort a list of lists

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to sort a list of lists


the data in the list is something like this
{[def,ghi,xml],
[abc,ghi,cde],
[cat,ghi,der]}
I want the data to be
{[abc,ghi,cde],
[cat,ghi,der],
[def,ghi,xml],}
I am not able to use Collections.sort(listOfLists)
Any help is greatly appreciated.
Thanks in advance.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to tell the Collections how you want this list to be sorted. What about making a Comparator<List<String>> ?
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
Hey can you please help me with some sample code.
 
Marshal
Posts: 70384
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sneha Kashyap wrote:Thanks
Hey can you please help me with some sample code.

No. We believe people learn better if they show us what ideas they have first.

Write whatever you have: if you have no code, write down in small words how to sort your list of lists. Note this text with the lines is full of small words.
 
Ranch Hand
Posts: 111
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you want to sort the lists based on the 1st String they contain (at least, this is my understanding), have a class implement Comparator<List<String>> as Christophe has suggested, in its method extract the 1st item from each list, and (since Strings are Comparable) just invoke Be sure to handle the case when either of the lists is empty.
Pseudo-code:
 
Sheriff
Posts: 22005
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given two List<String> objects the main question is indeed how they should be compared. The following is an often used algorithm (pseudo code):
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic