• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

sorting arrayList of a class according to a field  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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: 61690
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your List contains no duplicates, you can use a SortedSet.
 
Campbell Ritchie
Marshal
Posts: 61690
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!