Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: sort according

  1. #21
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    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
    Generally, you want to implement Comparable if your class has a "natural ordering" -- meaning there is some obvious way that one object should come before or after another. Now your post says "according to title" but is that always going to be the case? If you really want your Book objects to always be sorted by title, then you can have your Book class implement Comparable, and provide a simple compareTo method, based on your title:
    Java Code:
            public int compareTo(Book that) {
    		// TODO: remove articles "A" and "The" from titles before comparing
                    return this.getTitle().compareTo(that.getTitle());
            }
    On the other hand, if you might want to sort Book objects sometimes by author name, or by ISBN number, or by some other field, then you create a set of classes, each of which implements Comparator, and make those classes available to code that would want to sort your Book objects. One good way to do this is with an "anonymous inner class" like this:
    Java Code:
    public class Book {
            private String title;
            private String author;
            private int year;
    [COLOR="Blue"]        public static final Comparator<Book> BY_AUTHOR = new Comparator<Book>() {
                    @Override
                    public int compare(Book b1, Book b2) {
                            return b1.getAuthor().compareTo(b2.getAuthor());
                    }
            };
            // TODO: consider providing similar BY_TITLE, BY_YEAR and BY_EDITION instances
    [/COLOR]
            ...
    }
    If your class implements Comparable, then other code can do things like:
    Java Code:
            List<Book> bookList = new ArrayList<Book>();
            // code that fills the list with Book objects, then...
            Collections.sort(bookList);
    If you provide Comparator instances, then other code can do:

    Java Code:
            List<Book> bookList = new ArrayList<Book>();
            // code that fills the list with Book objects, then...
            Collections.sort(bookList, Book.BY_AUTHOR);
    Note that it is perfectly legal to do both -- implement Comparable and provide Comparator instances for alternate sortings.

    -Gary-
    Last edited by gcalvin; 01-15-2011 at 08:40 PM. Reason: style fixes, missing semicolon

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

    Default

    ok, it worked now, i used the selection sorting algorithim to sort it and using the compareToByTitle method that i did in the previous method, and heres what came up with me:

    Java Code:
    public void sortByTitle ()
    {
        selectionSort1(books);
    }
        
    private static void selectionSort1 (Book [] books)
    {
        for ( int i = 0; i < books.length - 1; i++)
        {
            for ( int j = i + 1; j < books.length; j++)
            {
                if (books[i].compareToByTitle(books[j]) == 1)
                {
                    Book temp = books[i];
                    books[i] = books[j];
                    books[j] = temp;
                }
            }
        }
    }
    and it worked fine with me

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

    Default

    Quote Originally Posted by aizen92 View Post
    ok, it worked now, i used the selection sorting algorithim to sort it and using the compareToByTitle method that i did in the previous method, and heres what came up with me:

    Java Code:
    public void sortByTitle ()
    {
        selectionSort1(books);
    }
        
    private static void selectionSort1 (Book [] books)
    {
        for ( int i = 0; i < books.length - 1; i++)
        {
            for ( int j = i + 1; j < books.length; j++)
            {
                if (books[i].compareToByTitle(books[j]) == 1)
                {
                    Book temp = books[i];
                    books[i] = books[j];
                    books[j] = temp;
                }
            }
        }
    }
    and it worked fine with me
    But now you needed to implement your own sorting method (and an inefficient one it is). Why didn't you read all the hints and implemented a simple Comparator<Book> class? For a large amount of books your sort method is going to take a substantial amount of time, it is O(n^2). And what if you want to sort other things? Are you going to re-implement it for those other things? You have ignored all forms of Object Oriented Programming.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    well, im a beginner in the whole programming thing, and i didnt understand the examples above, and the whole comperator thing, so i tried to do some messing and figured out this one

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

    Default

    Quote Originally Posted by aizen92 View Post
    well, im a beginner in the whole programming thing, and i didnt understand the examples above, and the whole comperator thing, so i tried to do some messing and figured out this one
    Here's a hint: implement a method that takes two parameters, a and b, both Books; return a positive number if Book a's title is larger than Book b's title, return zero is both titles are equal, otherwise return a negative number. If you have done that you have done 90% of the work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    create a comparator like this
    Java Code:
    class TitleComparator implements Comparator<Book>
    {
    public void compare(Book b1, Book b2)
    	{
    	return (b1.getTitle()).comapreTo(b2.getTitle())
    	}
    }
    then u can call
    -------------------------
    Java Code:
    Arrays.sort(books,new TitleComparator());
    i think it will work

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

    Default

    Aw, what a give away ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

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
  •