How to sort an array that is combined by & ldquo; Null & rdquo; And Cordes?

advertisements

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!


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()));