Results 1 to 14 of 14
Thread: Sort an ArrayList
- 02-10-2011, 09:59 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Sort an ArrayList
Hi!
Is there any function/method in the arraylist class able to sort an arraylist like this?
arraylist
Sorted arraylistJava Code:[0] = object 1 [1] = object 2 [2] = null [3] = object 4 [4] = null [5] = object 6 [6] = object 7
Java Code:[0] = object 1 [1] = object 2 [2] = object 4 [3] = object 6 [4] = object 7
Edit:
I tried this, but the sort part don't work.
Java Code:arrayList.trimToSize(); arrayList.sort();Last edited by überfuzz; 02-10-2011 at 10:21 AM. Reason: Forgot my trials
- 02-10-2011, 10:23 AM #2
you can create yourself comparator implementation Comparator<> interface and it use in your code as follows Collections.sort(List, Comparator);
Skype: petrarsentev
http://TrackStudio.com
- 02-10-2011, 10:44 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Thinking about it, it not really a sort stunt I'm trying to pull. I just want to reindex the array. Erasing all index corresponding to a value of null, or in my case no object.
- 02-10-2011, 10:50 AM #4
So and do you have some problems with that?
Can you show your code what are you doing.Skype: petrarsentev
http://TrackStudio.com
- 02-10-2011, 11:04 AM #5
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
you can use COllections.sort(ArrayList_obj)
- 02-10-2011, 11:08 AM #6
The any case him need implementations or yourself comparator or implementation Compare interface for object that use method Collection.sort(List)
Last edited by Petr; 02-10-2011 at 11:10 AM.
Skype: petrarsentev
http://TrackStudio.com
- 02-10-2011, 11:16 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2011, 11:18 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Since I didn't understand your posts I came up with an other way of solving this. For now I'll just set status == false on my objects, then I use a if statement to check status.
Thanks
- 02-10-2011, 11:30 AM #9
see
Java Code:package javatalk; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortEx { private static class User { private String name; private String soname; private User(String name, String soname) { this.name = name; this.soname = soname; } public String getName() { return name; } public String getSoname() { return soname; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (name != null ? !name.equals(user.name) : user.name != null) return false; if (soname != null ? !soname.equals(user.soname) : user.soname != null) return false; return true; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (soname != null ? soname.hashCode() : 0); return result; } @Override public String toString() { return "User: name - " + name + "; soname - " + soname; } } private static class UserCompare implements Comparator<User> { @Override public int compare(User o1, User o2) { if (o1 == null && o2 == null) { return 0; } else if (o1 == null) { return -1; } else if (o2 == null) { return 1; } else { return o1.getName().compareTo(o2.getName()); } } } public static void main(String[] arg) { List<User> users = new ArrayList<User>(); users.add(new User("Petr", "Pert")); users.add(null); users.add(new User("User_3", "User_3")); users.add(new User("User_2", "User_2")); users.add(null); users.add(new User("User_1", "User_1")); users.add(new User("Auser", "Auser")); printList(users); Collections.sort(users, new UserCompare()); printList(users); } private static void printList(List<User> list) { for (User user : list) { System.out.println(user); } System.out.println(); } }Skype: petrarsentev
http://TrackStudio.com
- 02-10-2011, 11:55 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
@Petr, spoonfeeding code like you did is not going to help the OP much; give a man a fish, or teach him to fish etc. Also, spoonfeeding is considered not done in this forum (and other forums).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2011, 12:50 PM #11
not methods or functions but interfaces called Comparator and Comparable. do you know why: imagine your object has multiple members. how should the sort method know which member(s) to use for sorting? right: by implementing the interface Comparator or Comparable you specify how your objects are to be sorted. for and example see the post of petr.
- 02-10-2011, 12:57 PM #12
Last edited by j2me64; 02-10-2011 at 01:00 PM.
- 02-10-2011, 01:14 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
In this case (List elements can be null) it is no use to make the elements implement the Comparable<T> interface, because a null element may be chosen as a target by the sort method to call the Comparable.compareTo( ... ) method on. A Comparator<T> has to do the job.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-10-2011, 01:18 PM #14
furthermore, even if you are a experienced java programmer "the things you think you know might work just a little different than you imagined. It isn't enough to be able to get your code to work correctly; you must understand the core fundamentals in a deep way " (OCP Java SE 6 Programmer Practice Exams, p. xv). so it doesn't hurt to open the the api-html about Arrays or Collections.Last edited by j2me64; 02-10-2011 at 01:21 PM.
Similar Threads
-
Problem arraylist result when use Collections.sort
By i4ba1 in forum Advanced JavaReplies: 4Last Post: 02-09-2011, 08:00 AM -
Arraylist problem Sort Date , alphabetically
By ob3lix in forum New To JavaReplies: 1Last Post: 11-26-2010, 03:55 PM -
ArrayList sort
By Dipke in forum New To JavaReplies: 1Last Post: 08-23-2010, 01:01 PM -
how to sort an arraylist?
By zhangster in forum New To JavaReplies: 5Last Post: 03-19-2010, 08:01 AM -
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks