Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sort an arraylist of custom objects

 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,
I have a array list that contains a number of custom objects.
My object has the below structure :
myObj.ID
myObj.Name
myObj.Date

is there a way i can sort this array list by id or name? The data comes from a web service not in an ordered way.

Thanks in advance.
 
Vishal Trivedi
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii


Why dont you try creating a comparator class.
The Class implements Comparator and its method compare to do comparison ...
you can make a condition here that can do sorting based on your preference.
Then call Collection.sort(urList,new instanceOfComparator());
you can modify the constructor to pass your preference like
Collection.sort(urList,new instanceOfComparator("myPrefernce"));



Just dont forget to use compareTO for comparison of String to have more accurate result.

Please correct me if i am wronng or have made a wrong interpretation of your question.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i did what you said and it works perfectly. One more question, not opening new post because it is for the same list of objects, how can i filter this list of objects from text in a edittext?
Can this be done dynamically or is there another way?

thanks in advance.
 
Vishal Trivedi
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry i dint not understand your question. properly i guess


you can use an AutoCompleteTextView instead of EditText this will work just like conact list from your cell phone fetching data dynamically from a predefined list as you type.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a array list that contains a number of custom objects.
My object has the below structure :
myObj.ID
myObj.Name
myObj.Date

is there a way i can filter this array by its name or date in any way?
 
Vishal Trivedi
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you not introduce a radoButton group containing the list of elements(name and Date) that may be used for sorting.And based on user's selection the key for sorting could be set.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am actually want help on how to filter the objects in the arraylist, my sorting function works.

thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic