Nabil EHL
Nabil EHL

Reputation: 31

How to sort an array that is combined by "null" and Strings?

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!

Upvotes: 3

Views: 216

Answers (4)

Skym0sh0
Skym0sh0

Reputation: 315

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 :)

Upvotes: 3

Nick Suwyn
Nick Suwyn

Reputation: 511

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

Upvotes: 2

diutsu
diutsu

Reputation: 103

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

Upvotes: 4

Geny Herrera
Geny Herrera

Reputation: 66

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

Upvotes: 2

Related Questions