Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: sort according

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default sort according

    Hello,
    I have an array of Book (a class i made, containing title, author name, year, and edition)
    and i want to sort them according to the title

    i saw it in the java doc the following method:

    public static <T> void sort(T[] a, Comparator<? super T> c)

    if someone knows how to use, can u explain it to me, i read its description but didnt understand it

    thanks in advance

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

    Default

    Quote Originally Posted by aizen92 View Post
    Hello,
    I have an array of Book (a class i made, containing title, author name, year, and edition)
    and i want to sort them according to the title

    i saw it in the java doc the following method:

    public static <T> void sort(T[] a, Comparator<? super T> c)

    if someone knows how to use, can u explain it to me, i read its description but didnt understand it
    It looks more intimidating than it actually is: that method sorts an array of type T (Books in your case) and it also takes a Comparator that can handle objects of type T (or subclasses thereof). A Comparator is an interface (look up its API documentation) that you have to implement so you can pass it to that sort method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    Hello,
    I have an array of Book (a class i made, containing title, author name, year, and edition)
    and i want to sort them according to the title

    i saw it in the java doc the following method:

    public static <T> void sort(T[] a, Comparator<? super T> c)

    if someone knows how to use, can u explain it to me, i read its description but didnt understand it

    thanks in advance
    You could either make your Book class implement Comparable<Book> and give it a compareTo(Book otherBook) method, or you could create a small utility class that extends Comparator<Book> and has one single method,

    Java Code:
    public int compare(Book book1, Book book2) {
      // return a 1 if if book1 is > book2, -1 if book1 < book2, or 0 if equal.
    }

  4. #4
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    see examples in the link below. That should help you
    Java object sorting example (Comparable and Comparator)

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well actually, in my case its gonna be more than 2 books, like 4 or 5 or 6 or more

    i was wondering if there was a method like to give it an array to compare, and a condition in the object (in my case Book) as parameters, and does it, or if someone can help me with an algorithm if not

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

    Default

    Quote Originally Posted by aizen92 View Post
    well actually, in my case its gonna be more than 2 books, like 4 or 5 or 6 or more

    i was wondering if there was a method like to give it an array to compare, and a condition in the object (in my case Book) as parameters, and does it, or if someone can help me with an algorithm if not
    If you can comparre two Books you can sort a zillion of them, that's what those Comparator<Book> objects are for (or make Book ojbects implement Comparable<Book> themselves as Fubarable suggested). Read the API documentation before you go on.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    I read the api many times, and tried to mess with the code, hoping it would work some way or another but cant
    and i saw the example nn12 gave me, but got me lost, if someone has an simpler example and an for it to be on a string, since the whole int thing too got me lost for me to see and check out, I would appreciate it

    also could there be another way than this comparable or not?
    Last edited by aizen92; 01-15-2011 at 10:56 AM.

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

    Default

    Quote Originally Posted by aizen92 View Post
    I read the api many times, and tried to mess with the code, hoping it would work some way or another but cant
    and i saw the example nn12 gave me, but got me lost, if someone has an simpler example and an for it to be on a string, since the whole int thing too got me lost for me to see and check out, I would appreciate it

    also could there be another way than this comparable or not?
    So you throw your hands up in the air and want us to do it for you? Better show us what you have tried and what failed in your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    its not that i want you to do it, all i want is somebody to at least guide me on how to do it

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You understand of course that no one is going to re-write a tutorial that is already out there and already far better written than anything we could do. Why don't you just show us what you've tried and what's not working as has been requested? You haven't even told us what specifically about these constructs it is that you don't understand. Again, show us your attempt.

  11. #11
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    i still havent written anything for this particular method, becuase i dont know from where to start.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    i still havent written anything for this particular method, becuase i dont know from where to start.
    Then you've nothing to lose by experimenting. You can probably do more than you realize.

  13. #13
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well, can i get like a starting point, from where should i start
    my problem with programming is sometimes i dont know from where to start

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

    Default

    Quote Originally Posted by aizen92 View Post
    well, can i get like a starting point, from where should i start
    my problem with programming is sometimes i dont know from where to start
    Start by implementing the Comparator interface. <--- starting point.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For a start, have Book implement Comparable<Book>. Then give Book a compareTo method:

    Java Code:
    public int compareTo(Book otherBook) {
       // you must add code that allows you to rank the current Book
       // with the otherBook.  What are you using to sort them?
    
       // based on this criterion,
       // the method will return 1 if this Book > otherBook
       // the method will return -1 if current Book < otherBook
       // the method will return 0 if current Book == otherBook
    }
    So what are you using as your basis of comparison of one Book vs. another?

    edit: LOL, Jos and I gave competing advice! Regardless, you can go either way -- with Comparable or Comparator, your choice. Just try, just try.

  16. #16
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by aizen92 View Post
    well, can i get like a starting point, from where should i start
    my problem with programming is sometimes i dont know from where to start
    I will give you a starting point. Ask google next time. there are lots of articles and materials about Java sorting more than what people can provide you here.

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

    Default

    Quote Originally Posted by Fubarable View Post
    edit: LOL, Jos and I gave competing advice! Regardless, you can go either way -- with Comparable or Comparator, your choice. Just try, just try.
    Well, the OP started with Comparator<T> in the original post and obedient little puppy that I am, I sticked to it ;-) On second thought, if the OP wants a configurable thing (comparing on either title, or name etc.) maybe a Comparator<T> is better ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Thnx guys for the help, i did the interface and a compareTo method until now, I'll have to continue it after around half an hour, as i have some things to do, but ill start on it when im back, and ill post my results
    thnx uys for helping

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aizen92 View Post
    Thnx guys for the help, i did the interface and a compareTo method until now, I'll have to continue it after around half an hour, as i have some things to do, but ill start on it when im back, and ill post my results
    thnx uys for helping
    Great, and good luck with the rest. Please let us know how things turn out.

  20. #20
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Okay, i tried with the comparable and the comparater but didnt work, sorry i really tried with it, anyway i figured that i have in my other class a method that compare 2 books by title, so y not use it, heres my 2 classes and my attempt:

    Java Code:
    public class Book
    {
        private String title;
        private String authorName;
        private int year;
        private int edition;
        
        public Book (String t, String n, int y)
        {
            title = t;
            authorName = n;
            year = y;
            edition = 1;
        }
        
        public Book (String t, String n, int y, int e)
        {
            title = t;
            authorName = n;
            year = y;
            edition = e;
        }
        
        public String getTitle ()
        {
            return title;
        }
        
        public String getAuthorName ()
        {
            return authorName;
        }
        
        public int getYear ()
        {
            return year;
        }
        
        public int getEdition ()
        {
            return edition;
        }
        
        public void setTitle (String t)
        {
            title = t;
        }
        
        public void setAuthorName (String n)
        {
            authorName = n;
        }
        
        public void setYear (int y)
        {
            year = y;
        }
        
        public void setEdition (int e)
        {
            edition = e;
        }
        
        public boolean equals (Book b)
        {
            if (this.title.equalsIgnoreCase(b.getTitle()) && this.authorName.equalsIgnoreCase(b.getAuthorName()) && this.year == b.getYear() && this.edition == b.getEdition())
            {
                return true;
            }else{
                return false;
            }
        }
        
        public String toString ()
        {
            String str = "Book information:\nTitle: " + title + "\nAuthor's Name: " + authorName + "\nYear: " + year + "\nEdition: " + edition;
    
            return str;
        }
        
        public int compareToByTitle (Book b)
        {
            int comp = title.compareToIgnoreCase(b.getTitle());
            
            return comp;
        }
        
        public int compareToByAuthor (Book b)
        {
            int comp = authorName.compareToIgnoreCase(b.getAuthorName());
            
            return comp;
        }
    }
    Java Code:
    public class BookCollection 
    {
        private Book [] books;
        private int size;
        
        public BookCollection (int len)
        {
            size = len;
            books = new Book [size];
        }
        
        public void add (Book b)
        {
            if ( books[checkArray()] == null )
            {
                books[checkArray()] = b;
            }else{
                Book [] temp = new Book [books.length];
                
                for ( int i = 0; i < temp.length; i++)
                {
                    temp [i] = books [i];
                }
                
                books = new Book [books.length + 1];
                
                for ( int i = 0; i < books.length - 1; i++)
                {
                    books [i] = temp [i];
                }
                
                books [books.length - 1] = b;
            }
        }
        
        private  int checkArray ()
        {
            int index = 0;
            
            for( int i = 0; i < books.length; i++)
            {
                if ( books[i] == null )
                index = i;
            }
            
            return index;
        }
        
        public void remove (Book b)
        {
            if ( checkArray(b))
            {
                Book [] temp = new Book [books.length - 1];
                
                int index = 0;
                
                for ( int i = 0; i < temp.length; i++)
                {
                    if ( books[index] != b)
                    {
                        temp[i] = books[index];
                    }else{
                        temp[i] = books[index + 1];
                    }
                    index++;
                }
                
                books = new Book [temp.length];
                
                for ( int i = 0; i < books.length; i++)
                {
                    books[i] = temp[i];
                }
            }
        }
        
        private boolean checkArray (Book b)
        {
            boolean found = false;
            
            for ( int i = 0; i < books.length; i++)
            {
                if ( books[i] == b )
                {
                    found = true;
                    break;
                }else{
                    found = false;
                }
            }
            
            return found;
        }
    }
    and here is my attempt of sorting by title, it sould be in the BookCollection class

    Java Code:
    public void sortByTitle ()
        {
            Book [] temp = new Book [books.length];
            
            for ( int i = 0; i < temp.length; i++)
            {
                temp[i] = books[i];
            }
            
            books = new Book [temp.length];
            
            for ( int i = 0; i < temp.length - 1; i++)
            {
                if ( temp[i].compareToByTitle(temp[i + 1]) == -1)
                {
                    books[i] = temp[i];
                }
            }
        }
    but the thing is it didnt work, i used an example like, have an array and store in it 3 books, at random like book2, book3, book1, and invoked the sorting method, however all it did was put 1 book in the array at position 1, and the rest are nulls
    is my attempt here at the right path or is it wrong?

Page 1 of 2 12 LastLast

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. tree sort
    By bigdoggy in forum New To Java
    Replies: 5
    Last Post: 05-01-2008, 05:52 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  5. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 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
  •