Jacob
Jacob

Reputation: 523

Sort an String-Array in the right order

I have tried to sort my array in the right order. I have stored 3 variables (1 int, 1 String, 1 float) in one field of the array. I have tried to use the native sort method, but my output is not sorted the right way:

[1 ,Agavendicksaft  ,0.180
, 1 ,Agavendicksaft  ,0.284
, 100 ,Röstzwiebel ,0.057
, 103 ,Salz fein ,6.220
, 103 ,Salz fein ,6.452
, 104 ,Salz grob ,0.490
, 114 ,Sesam ,0.735
, 114 ,Sesam ,1.742
, 115 ,Soja Granulat ,43.788
, 116 ,Sonnenblumenkerne ,0.267
, 116 ,Sonnenblumenkerne ,3.636
, 12 ,BAS hell ,0.975
, 12 ,BAS hell ,6.996
, 139 ,Vanille Aroma ,0.068
, 140 ,Weizenmehl Type W1600 ,1.163
, 140 ,Weizenmehl Type W1600 ,1.927
, 141 ,Weizenmehl Type W700 ,138.127
, 141 ,Weizenmehl Type W700 ,45.158
, 142 ,Walnüsse ,0.228
, 144 ,Wiechert Glutenfei ,1.160
, 145 ,Wienerwurst Stange ,0.100
, 150 ,Zitronen Aroma ,0.068
, 151 ,Zucker Normalkristall ,1.039
, 153 ,Wasser ,167.202
, 21 ,Dinkel Flocken ,0.347
, 24 ,Eier ganz ,0.453
, 26 ,Eigelb ProOvo ,0.365
, 29 ,Fenchel ganz ,0.105
, 36 ,Hafer ganz ,3.078
, 47 ,Hirse ganz ,0.133
, 49 ,Honig ,0.186]

So I have two questions:

  1. How could I sort the array right?
  2. How could I combine multiple entries to one entrie? f.e. not 140, 1.163 140, 1.927 => 140, 3,09 (I cannot do that earlier in my code because the array is a combination of 3 sql-result-arrays)

Here is my code:

s = Results2String(resultSet);

splitResult = s.split("/");
System.out.println(s);

s = null;
s = Results2String(resultSet2);
splitResult2 = s.split("/");
System.out.println(s);

s = null;
s = Results2String(resultSet3);
splitResult3 = s.split("/");
System.out.println(s);

System.out.println(splitResult3[0]);

preResult = new String[splitResult.length + splitResult2.length];

System.arraycopy(splitResult, 0, preResult, 0, splitResult.length);
System.arraycopy(splitResult2, 0, preResult, splitResult.length, splitResult2.length);
System.out.println(Arrays.toString(preResult));

result = new String[splitResult.length + splitResult2.length + splitResult3.length];
System.arraycopy(preResult, 0, result, 0, preResult.length);
System.arraycopy(splitResult3, 0, result, preResult.length, splitResult3.length);

Arrays.sort(result);
System.out.println(Arrays.toString(result));

Upvotes: 0

Views: 266

Answers (3)

PoByBolek
PoByBolek

Reputation: 3905

I assume that one of each int, String and float belong together.

So you should put them in one class and implement Comparable (documentation):

public class Food implements Comparable<Food> {
    private int id;
    private String name;
    private float score; // or maybe kcal... ?

    public Food(int id, String name, float score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Food other) {
        return id - other.getId();

        // use the following if you want to sort by name instead
        // return name.compareTo(other.getName());
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Then you can sort an array of Foods....


How to use

I'm still not sure what your resultSets are (and why you have three of them) but I assume that they are ResultSets from a database query. Furthermore I assume that the id-name-score-combinations are actually rows in a database.

You could then go through those ResultSets and put the values of each row in a Food object:

 ResultSet resultSet; // filled somewhere else
 List<Food> food = new ArrayList<Food>();

 while (resultSet.next()) {
     int id = resultSet.getInt(0); // assuming the ids are in the first column
     String name = resultSet.getString(1); // assuming the names are in the second column
     float score = resultSet.getFloat(2); // assuming the scores are in the third column

     food.add(new Food(id, name, score));
 }

 Collections.sort(food);

I hope this helps...

However, if your resultSets are really ResultSets from a database query, then you should probably sort your values in your SQL query with ORDER BY...

Upvotes: 8

ThePoltergeist
ThePoltergeist

Reputation: 182

Create an class with those three attributes.

public class Example{
    private int field1;
    private String field2;
    private double field3;
    .....
}

Fill you array with Objects made of this class then sort your array with a Comparator for this class that compares the String.

Upvotes: 0

DwB
DwB

Reputation: 38290

You need to write a comparator method and use the Collections.sort(List<T>, Comparator<? super T> method. Here is a link to the Collections java doc online.

inside your comparator, split the line into 3 parts (int, string, float) and compare them as you see fit.

Upvotes: 0

Related Questions