• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting arrayList of a class according to a field

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
we have an Arraylist<Tweet>, and this class is defined as followe:

now if we implement the method comparteTo, then will it be sorted automatically?
I want to sort this array by any insert.

Thank you in advance..
best,
David
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What array? You don't have an array. You have a List.

No, it will not iterate in order of sorting. It will iterate in insertion order. If you want iteration of that List in sorted order you must call a sort method:-
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, yes i meant ArrayList.

but I want an efficient way, when we have lots of data, then sorting by any insert is not efficient.


Campbell Ritchie wrote:What array? You don't have an array. You have a List.

No, it will not iterate in order of sorting. It will iterate in insertion order. If you want iteration of that List in sorted order you must call a sort method:-

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check out Collections#sort(List<T> list,Comparator<? super T> c);
More detals here: http://www.vogella.com/tutorials/JavaCollections/article.html#collectionssort
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, but I dont want to sort all of the list after each insert, it is not efficient in the big data set. this list will be just partially unsorted, it doesn't need to be sorted completely.

isn't there any other way?

Maneesh Godbole wrote:Also check out Collections#sort(List<T> list,Comparator<? super T> c);
More detals here: http://www.vogella.com/tutorials/JavaCollections/article.html#collectionssort

 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you perform more writes than reads to your list, then you can choose to only sort on data retrieval from that list. And then don't sort after an addition to the list.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your List contains no duplicates, you can use a SortedSet.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through the Collections class and see whether there is any way to sort part of the List. I don't think there is. You can create a sublist, sort that, and copy it back, however.

In Java8 you can probably use a Stream. Maybe something like this:-
myList.stream().sorted().toArray()
…which gives an array. Cast that array to Tweet[] and you can use it to create a List.

There must be a better way to create a List. I shall challenge you to find it. A good source about all things List‑y is the Java Tutorials, so start there.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic