-2

Hello everyone I have a class named GEN and I have an array of this what I want to to is sort this array based on time variable So theone with the shortest time must be the gend[0]. I hope I explained my question clear and I am using NetBeans

 public class GEN
{
    int[][] mark=new int[TIMELIMIT][N+1];
    int[][] dimark=new int[TIMELIMIT][N+1];
    int time=0;
    int[] touched=new int[N+1];
}
public GEN[] gend=new GEN[GENNUM];
Cœur
  • 37,241
  • 25
  • 195
  • 267
esezen
  • 13
  • 3
  • possible duplicate of [Java Sorting: sort an array of objects by property, object not allowed to use Comparable](http://stackoverflow.com/questions/12449766/java-sorting-sort-an-array-of-objects-by-property-object-not-allowed-to-use-co) and of dozens of similar questions. – JB Nizet Feb 28 '14 at 17:34
  • I'll add that there is more than one duplicate out there. Some keywords you might need: _sort_, _array_, _java_, _object_, _comparator_ etc. – keyser Feb 28 '14 at 17:35
  • Implement comparable on GEN class using time attribute and use Arrays.sort to sort. – Sanjeev Feb 28 '14 at 17:36

2 Answers2

0

You need to implement Comparable.

public class GEN implements Comparable<GEN>
{
    int[][] mark=new int[TIMELIMIT][N+1];
    int[][] dimark=new int[TIMELIMIT][N+1];
    int time=0;
    int[] touched=new int[N+1];

    int compareTo(GEN obj)
    {
        // compareTo returns a negative number if this is less than obj, 
        // a positive number if this is greater than obj, 
        // and 0 if they are equal.
        return this.time - obj.time;
    }
}

public GEN[] gend=new GEN[GENNUM];

By implementing Comparable, you are then able to use Arrays.sort() to sort your array once it is populated with GEN objects. The sort function uses compareTo to determine the proper ordering of the objects. Thus, once you've implemented Comparable, you only need to write:

Arrays.sort(gend);
Luke Willis
  • 8,429
  • 4
  • 46
  • 79
  • Actually I am new with Java so could you give an full example with sort, and thank you a lot – esezen Feb 28 '14 at 17:40
  • When i use it i get this : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at proce.main.Main$GEN.compareTo(Main.java:65) at proce.main.Main$GEN.compareTo(Main.java:57) at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290) – esezen Feb 28 '14 at 18:28
  • Thanks a lot and i figured out what the problem was, thank you all – esezen Feb 28 '14 at 20:46
0

Seriously, you should check out the Java docs link Luke Willis posted in his answer, its pretty impressive to see the sheer number of classes (a good chunk of which are used very frequently) which are "Known implementing classes". If you go over that list and consider how powerful compare can be for the classes you are familiar with (if you are familiar with auto-boxing/unboxing then you will be, if not, you can click on those classes at that spot in the Java doc.

You can sit there and try to figure out for the first time how to code an algorithm that sorts for example an ArrayList<SomeObjectType> arrList based on an instance variable of each object inside, such as Card objects' primitive int faceValue...

...60+ minutes later since you are unfamiliar with how to create a sorting algorithm it will take a while. Also, it will be a hefty chunk of somewhat obfuscated code.

...or you could just read up on Comparable and implement it.

Edit: Not to say that making an algorithm by hand would be a bad thing, it would definitely be a good learning experience, but Comparable is such a powerful and condense tool to implement in your code.

  • When i use it i get this : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at proce.main.Main$GEN.compareTo(Main.java:65) at proce.main.Main$GEN.compareTo(Main.java:57) at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290) – esezen Feb 28 '14 at 18:29