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!

5
  • 2
    What is contained in the Karte class? Commented Apr 12, 2016 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. Commented Apr 12, 2016 at 20:03
  • Find an example of a Comparator in the accepted answer here: stackoverflow.com/questions/27556104/… Commented Apr 12, 2016 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 ! Commented Apr 12, 2016 at 20:13
  • About getName() == karte.getName() read "How do I compare strings in Java?". Also if (condition){ return true; } else {return false;} can be rewritten as simple return condition. Commented Apr 12, 2016 at 20:25

4 Answers 4

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()));
Sign up to request clarification or add additional context in comments.

2 Comments

This does not solve OP's main problem of null values, as it will cause a NullPointerException as soon as it hits one
You are right, see answer Skym0sh0 to fix the NPE problem.
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 :)

1 Comment

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

4 Comments

What happens if one or both Karte objects have a null name?
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)
If you are implementing a comparable, why not do it correctly so as to handle null cases?
@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!!
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());
    }});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.