Results 1 to 14 of 14
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Sort an ArrayList

    Hi!

    Is there any function/method in the arraylist class able to sort an arraylist like this?

    arraylist
    Java Code:
    [0] = object 1
    [1] = object 2
    [2] = null
    [3] = object 4
    [4] = null
    [5] = object 6
    [6] = object 7
    Sorted arraylist
    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 11:21 AM. Reason: Forgot my trials

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    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.

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    So and do you have some problems with that?
    Can you show your code what are you doing.
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    you can use COllections.sort(ArrayList_obj)

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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 12:10 PM.
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by jomypgeorge View Post
    you can use COllections.sort(ArrayList_obj)
    You can't use it because the List can contain null elements. The OP has to implement a simple Comparator<T> that anticipates on the fact that one of, or both of, its arguments can be null in the compare( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    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

  9. #9
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    @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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by überfuzz View Post
    Is there any function/method in the arraylist class able to sort an arraylist like this?
    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.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Petr View Post
    @Override
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    corresponding to the book "Effective Java" of J. Bloch the last if can be written in a more effective way with

    if (!(o instanceof User)) return false;

    since instanceof checks for null and instanceof.
    Last edited by j2me64; 02-10-2011 at 02:00 PM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    @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,

    Jos

    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 02:21 PM.

Similar Threads

  1. Problem arraylist result when use Collections.sort
    By i4ba1 in forum Advanced Java
    Replies: 4
    Last Post: 02-09-2011, 09:00 AM
  2. Arraylist problem Sort Date , alphabetically
    By ob3lix in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 04:55 PM
  3. ArrayList sort
    By Dipke in forum New To Java
    Replies: 1
    Last Post: 08-23-2010, 02:01 PM
  4. how to sort an arraylist?
    By zhangster in forum New To Java
    Replies: 5
    Last Post: 03-19-2010, 09:01 AM
  5. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •