3

I have got an array of 20:

private Karte[] deckArr;
deckArr = new Karte[20];

Now I want to sort the array by card-names every time a new card is added.

P.S. the cards are added 1 by 1 after clicking on a button, so there are empty spaces in the array.

Since the...

Arrays.sort(deckArr.getName());

...method does not work here I asked myself how it is done.

Karte(card) class:

package Model;

/**
 * Created by 204g07 on 18.03.2016.
 */
public class Karte implements ComparableContent<Karte>{
    private int schoenheit;
    private int staerke;
    private int geschwindigkeit;
    private int intelligenz;
    private int coolness;
    private int alter;
    private String seltenheit;
    private String name;

    public Karte(String pName, int pSchoenheit,int pStaerke,int pGeschwindigkeit, int pIntelligenz, int pCoolness, int pAlter, String pSeltenheit ) {
        name=pName;
        schoenheit=pSchoenheit;
        staerke=pStaerke;
        geschwindigkeit=pGeschwindigkeit;
        intelligenz=pIntelligenz;
        coolness=pCoolness;
        alter=pAlter;
        seltenheit=pSeltenheit;
    }
    //getter
    public int getSchoenheit(){
        return schoenheit;
    }
    public int getStaerke(){
        return staerke;
    }
    public int getGeschwindigkeit(){
        return geschwindigkeit;
    }
    public int getIntelligenz(){
        return intelligenz;
    }
    public int getCoolness(){
        return coolness;
    }
    public int getAlter(){
        return alter;
    }
    public String getSeltenheit(){
        return seltenheit;
    }
    public String getName(){
        return name;
    }


    //setter
    public void setSchoenheit(int pSchoenheit){
        schoenheit = pSchoenheit;
    }
    public void setStaerke(int pStaerke){
        staerke = pStaerke;
    }
    public void setGeschwindigkeit(int pGeschwindigkeit){
        geschwindigkeit = pGeschwindigkeit;
    }
    public void setIntelligenz(int pIntelligenz){
        intelligenz = pIntelligenz;
    }
    public void setCoolness(int pCoolness){
        coolness = pCoolness;
    }
    public void setAlter(int pAlter){
        alter = pAlter;
    }
    public void setSeltenheit(String pSeltenheit){
        seltenheit = pSeltenheit;
    }
    public void setName(String pName){
        name = pName;
    }

    @Override
    public boolean isLess(Karte karte) {
        if (getName().compareTo(karte.getName()) < 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean isEqual(Karte karte) {
        return getName() == karte.getName();
    }

    @Override
    public boolean isGreater(Karte karte) {
        if (getName().compareTo(karte.getName()) > 0){
            return true;
        }else{
            return false;
        }
    }




}

Any help is appreciated!

Nabil EHL
  • 31
  • 5
  • 2
    What is contained in the Karte class? – Logan Apr 12 '16 at 20:01
  • 2
    You'll need a Comparator to compare the name values. Also, you cannot invoke instance methods of Karte on an array of Karte. – S.Klumpers Apr 12 '16 at 20:03
  • Find an example of a Comparator in the accepted answer here: http://stackoverflow.com/questions/27556104/how-to-sort-an-array-of-objects-containing-null-elements – Nick Suwyn Apr 12 '16 at 20:05
  • Before you sort it check that your have no null objects within your array, is using an array mandatory ? If not consider using a list ! – Benoit Vanalderweireldt Apr 12 '16 at 20:13
  • About `getName() == karte.getName()` read "[How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)". Also `if (condition){ return true; } else {return false;}` can be rewritten as simple `return condition`. – Pshemo Apr 12 '16 at 20:25

4 Answers4

4

Why not just use ArrayList instead? Easier to add, remove elements and you will never have empty slots.

Anyway to sort you can use Collections.sort like this:

deckArr = new ArrayList<Karte>();
Collections.sort(deckArr, Comparator.comparing(karte -> karte.getName()));
diutsu
  • 103
  • 1
  • 6
3

Java 8 offers a simple solution:

The Comparable Interface has a static method that creates a Comaprator with an extractor.

Comparator<Card> comp = Comparator.comparing(Karte::getName);

With this using a sorting method (e.g. Arrays.sort) is easy to call.

On top of that, to solve your nullpointer problem, the Comparator Interface offers another two functions: NullsLast and nullsFirst.

Comparator<Card> comp = Comparator.nullsLast(Comparator.comparing(Card::getName));

For me this looks like the easiest solution to your question :)

Skym0sh0
  • 305
  • 3
  • 6
  • 1
    Your solution to the NPE only covers null values on the array. If you also want to fix null values on the Larte::getName you should use: `Comparator.nullsFirst(Comparator.comparing(Karte::getName,Comparator.nullsFirst(Comparator.naturalOrder())));` – diutsu Apr 13 '16 at 17:52
2

This should solve your problem. Implements the Comparable interface.

/**
 * Created by 204g07 on 18.03.2016.
 */
public class Karte implements Comparable<Karte>{
    private int schoenheit;
    private int staerke;
    private int geschwindigkeit;
    private int intelligenz;
    private int coolness;
    private int alter;
    private String seltenheit;
    private String name;

    public Karte(String pName, int pSchoenheit,int pStaerke,int pGeschwindigkeit, int pIntelligenz, int pCoolness, int pAlter, String pSeltenheit ) {
        name=pName;
        schoenheit=pSchoenheit;
        staerke=pStaerke;
        geschwindigkeit=pGeschwindigkeit;
        intelligenz=pIntelligenz;
        coolness=pCoolness;
        alter=pAlter;
        seltenheit=pSeltenheit;
    }
    //getter
    public int getSchoenheit(){
        return schoenheit;
    }
    public int getStaerke(){
        return staerke;
    }
    public int getGeschwindigkeit(){
        return geschwindigkeit;
    }
    public int getIntelligenz(){
        return intelligenz;
    }
    public int getCoolness(){
        return coolness;
    }
    public int getAlter(){
        return alter;
    }
    public String getSeltenheit(){
        return seltenheit;
    }
    public String getName(){
        return name;
    }


    //setter
    public void setSchoenheit(int pSchoenheit){
        schoenheit = pSchoenheit;
    }
    public void setStaerke(int pStaerke){
        staerke = pStaerke;
    }
    public void setGeschwindigkeit(int pGeschwindigkeit){
        geschwindigkeit = pGeschwindigkeit;
    }
    public void setIntelligenz(int pIntelligenz){
        intelligenz = pIntelligenz;
    }
    public void setCoolness(int pCoolness){
        coolness = pCoolness;
    }
    public void setAlter(int pAlter){
        alter = pAlter;
    }
    public void setSeltenheit(String pSeltenheit){
        seltenheit = pSeltenheit;
    }
    public void setName(String pName){
        name = pName;
    }

    public int compareTo(Karte karte) {
        return this.name.compareTo(karte.getName());
    }
}

Then you just need to call Arrays.sort(deckArr);

  • 3
    What happens if one or both Karte objects have a null name? – VGR Apr 12 '16 at 20:19
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320) at java.util.ComparableTimSort.sort(ComparableTimSort.java:188) at java.util.Arrays.sort(Arrays.java:1246) at Controller.Controller.addCardToDeck(Controller.java:82) at View.GUI.clickedAddButton(GUI.java:469) – Nabil EHL Apr 12 '16 at 20:23
  • If you are implementing a comparable, why not do it correctly so as to handle `null` cases? – Debosmit Ray Apr 12 '16 at 20:26
  • @DebosmitRay because I forgot to handle null case. NabilEHL you had a lot of examples here how to deal with null cases, sorry about that, my bad! I just try to help, but I forgot this basic and very important verification!! – Geny Herrera Apr 12 '16 at 20:57
2

You need to check for nulls and just call below--

Arrays.sort(deckArr, new Comparator<Karte>() {
    @Override
    public int compare(Karte karte1, Karte karte2) {
        if (karte1.getName() == null && karte2.getName() == null) {
            return 0;
        }
        if (karte1.getName() == null) {
            return 1;
        }
        if (karte2.getName() == null) {
            return -1;
        }
        return karte1.getName().compareTo(karte2.getName());
    }});
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
Nick Suwyn
  • 501
  • 1
  • 5
  • 21