Results 1 to 2 of 2
  1. #1
    TommyLR is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Help with sorting an object array.

    Java Code:
    package gestionecd;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    /**
     *
     * @author Tommy
     */
    public class CDmusical {
    
    private String name = "";
    private String genre = "";
    private Calendar date;
    
    
        CDmusicale(String name1, String genre1, Calendar date1){
            name = name1;
            genre = genre1;
            date = date1;
        }
    
        public String getinfo() {
             return name + " " +  genre + " " + String.valueOf(date.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(date.get(Calendar.MONTH) + "/" + String.valueOf(date.get(Calendar.YEAR)));
        }
    
        public int getyear(){
            return date.get(Calendar.YEAR);
        }
    
    
    }
    Java Code:
    package gestionecd;
    import java.util.*;
    public class ordinaarray {
    
        public static void ordinaarray(CDmusical[] array){
    
            CDmusical[] anarray = new CDmusical[array.length];
            anarray = array;
             for(int i = 1; i <= anarray.length - 1; i++){
                System.out.println("Informazioni CD " + i);
                System.out.println(anarray[i].getinfo());
            }
            System.out.println();
            System.out.println(" ORDINO ARRAY ");
            System.out.println();
            Arrays.sort(array);
           
            
            for(int iz = 1; iz <= anarray.length - 1; iz++){
                System.out.println("Informazioni CD " + iz);
                System.out.println(anarray[iz].getinfo());
            }
        }
    }
    Java Code:
    package gestionecd;
    import java.io.*;
    import java.util.*;
    
    
    public class CDtester {
        public static void main(String[] args) throws IOException{
    
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            int n = 0;
            String localname = "";
            String localgenre = "";
    
            /* INSERIMENTO AUTOMATICO */
    
            CDmusical [] arrayCD = new CDmusical[10];
            CDmusical p = new CDmusical("", "", new GregorianCalendar());
    
            arrayCD[1] = new CDmusical("Mutter", "Metal", new GregorianCalendar(2001, 1, 1));
            arrayCD[2] = new CDmusical("Herzeleid", "Metal", new GregorianCalendar(1995, 1, 1));
            arrayCD[3] = new CDmusical("Reise Reise", "Metal", new GregorianCalendar(2004, 1, 1));
            arrayCD[4] = new CDmusical("Rosenrot", "Metal", new GregorianCalendar(2005, 1, 1));
            arrayCD[5] = new CDmusical("Sehnsucht", "Metal", new GregorianCalendar(1998, 1, 1));
            arrayCD[6] = new CDmusical("The BEst", "Metal", new GregorianCalendar(1955, 1, 1));
            arrayCD[7] = new CDmusical("Oohmph", "Metal", new GregorianCalendar(1988, 1, 1));
            arrayCD[8] = new CDmusical("Warbringer", "Metal", new GregorianCalendar(1977, 1, 1));
            arrayCD[9] = new CDmusical("Warlock", "Metal", new GregorianCalendar(1909, 1, 1));
    
    
    
            /* INSERIMENTO MANUALE
            System.out.println("Quanti cd vuoi inserire?");
            n = Integer.parseInt(in.readLine());
            CDmusical [] arrayCD = new CDmusical[n+1];
            CDmusical p = new CDmusical("", "", new GregorianCalendar());
            for(int i = 1; i <= n; i++){
                arrayCD[i] = new CDmusical(localname, localgenre, new GregorianCalendar());
            }
           
            for(int i = 1; i <= n; i++){
            Calendar localdate1 = new GregorianCalendar();
            System.out.println("Informazioni CD " + i + " - Nome?");
            localname = in.readLine();
            System.out.println("Informazioni CD " + i + " - Genere?");
            localgenre = in.readLine();
            System.out.println("Informazioni CD " + i + " - Anno, Mese, Giorno pubblicazione?");
            localdate1.set(Integer.parseInt(in.readLine()), Integer.parseInt(in.readLine()), Integer.parseInt(in.readLine()));
            arrayCD[i] = new CDmusical(localname, localgenre, localdate1);
            }
            */
    
            ordinaarray.ordinaarray(arrayCD);
    
        }
    
    }

    I wonder how I could sort an object array (made by CDmusical objects) by the date.get(Calendar.YEAR)) method.

    I really can't get thru this.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Since you're using Arrays.sort() you can use the one that takes a Comparator.
    Inside that you can write the compare method to simply compare the getyear().

Similar Threads

  1. object list sorting
    By karq in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2010, 06:45 PM
  2. Sorting ArrayList by object data
    By drymsza1234 in forum New To Java
    Replies: 2
    Last Post: 04-15-2010, 02:22 AM
  3. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:42 AM
  4. Sorting ANY Object, and new Exceptions
    By Arsenic in forum New To Java
    Replies: 4
    Last Post: 03-05-2009, 08:15 PM
  5. URGENT: Sorting a vector of object by an element
    By doobybug in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:37 PM

Posting Permissions

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