• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort an arraylist that it is a different type based on array that also a different type  RSS feed

 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

My Question is how can i sort an Arraylist that it is a Different Type (Not string or nothing like that, i mean like custom item) based on array that different type (custom type)

I have no code but if there is some one that have any suggestions how to do it feel free to post here.

Thanks in advance
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by "...based on array that different type"?

You can write what's called a comparator. this basically takes two objects, and determines if one is greater than, equal to, or less than the other, based on whatever criteria you define. If I had "house" objects, I could write my comparator to decide based on price of the house, on their ages, on their number of bedrooms...or whatever I want.

I then pass this to the sort methods that already exist, and it does the rest.

I'm sure if you search this forum, you will find some other threads on this very topic.
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to sort ArrayList that based on the sort of Array
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:I want to sort ArrayList that based on the sort of Array


How did you sort the array in the first place? Did you use the java.util.Arrays class? And using either Comparable or a Comparator?

If so, you can sort the List, using the java.util.Collections class, using the same, either Comparable or Comparator.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:I want to sort ArrayList that based on the sort of Array

i'm not trying to be a pain, but that isn't really a spec. you're really just repeating what you already said in your first post.

Specs are supposed to be SPECIFIC. how do you sort one array (or ArrayList) based on the sort of another array? a specific example would help, but remember that is only part of a spec, not the complete spec.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!