-1

hello i have 2 string array

private String names[] = {
    "Drinks",
    "Bikes",
    "Cars"};

private String desc[] = {
    "this is drinks package",
    "package for bikes",
    "package cars"};

// now i used 
Arrays.sort(names);

Now that the names are in correct A - Z order, how can I sort "desc" array based on "names" array? For example after sorting array "names" result:

Bikes,Cars,Drinks

now i want the desc array to format same way so proper "desc" lie on proper "names". Result should be this:

names = "Bikes,Cars,Drinks"
desc = "package for bikes","package cars,this is drinks package"

can we do this ?

The marked is duplicated this is not duplicate of descending anyway

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

2 Answers2

1

Your goal is to link a key (Names array) to a value (descr array).

You can achieve it in two ways:

Use a bidimensional array:

String[][] myItems = new String[][] {
  {"Drinks", "This is drink package"},
  {"Cars", "package cars"},
  {"Bikes", "package for bikes"},
  ...
};

Now you can sort it following this example or any other else

Use AbstractMap.SimpleEntry

With this you simply have to create a one dimensional array of AbstractMap.SimpleEntry<String, String> and order it. you can follow the documentation I linked for this goal.

Hope this helped

NOTE

If there is no logic between the two arrays, you can't link them. in your example it could be done by searching in the second array the keywords of the first array, but since you didn't say anything about any "must have" for the desc array, we have to suppose that a "kid" entry could be linked to a "baby package" descr, so you can't find it by searching "kid". Hope I explained in a decent way

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • this does work but my custom listview gets messed up the reason is that i have made two different textviews in layout ,one for title and one for description now when i send this CustomList customList = new CustomList(this, names, desc); well later text is set by this way textViewName.setText(names[position]); textViewDesc.setText(desc[position]); and this is getting messed up any simple approach,something like if we can get the count position while sorting first array and use the same count to sort second one ? – AndroidGuy007 Oct 17 '16 at 16:15
1

Why not use classes?

As in Object-Oriented Principles, when you have data related to a specific topic, you put them in a class together.

Example:

NameDescription class implementing Comparable interface

public class NameDescription implements Comparable<NameDescription> {
    private String name;
    private String description;

    public NameDescription(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int compareTo(NameDescription o) {
        return name.compareTo(o.getName());
    }
}

Comparable interface provides the compareTo method which makes you able to define the comparison formula to compare the current object with another one (they don't even need to be of the same type). Arrays.sort method uses the compareTo method to sort the elements, so this way you have your data together and any sorting method will give you consistent results based on your compareTo method.

Sorter class:

public class Sorter {

    public static void main(String[] args) {
        NameDescription namesDescriptions[] = {
                new NameDescription("Drinks", "this is drinks package"),
                new NameDescription("Bikes", "package for bikes"),
                new NameDescription("Cars", "package cars"),
            };

        Arrays.sort(namesDescriptions);

        // ASCENDING
        for(int i=0; i<namesDescriptions.length; i++) {
            System.out.println(namesDescriptions[i].getName());
        }
        for(int i=0; i<namesDescriptions.length; i++) {
            System.out.println(namesDescriptions[i].getDescription());
        }

        System.out.println("---------");

        // DESCENDING
        for(int i=namesDescriptions.length-1; i>=0; i--) {
            System.out.println(namesDescriptions[i].getName());
        }
        for(int i=namesDescriptions.length-1; i>=0; i--) {
            System.out.println(namesDescriptions[i].getDescription());
        }

        // To save them in separate arrays
        String[] names = new String[namesDescriptions.length];
        String[] desc = new String[namesDescriptions.length];
        String[] imageid = new String[namesDescriptions.length];
        for(int i=0; i<namesDescriptions.length; i++) { 
            names[i] = namesDescriptions[namesDescriptions.length-i-1].getName();
            desc[i] = namesDescriptions[namesDescriptions.length-i-1].getDescription();
            imageid[i] = namesDescriptions[namesDescriptions.length-i-1].getimg();
        }
    }

}
  • thanks that works as expected i just modified with listview ,marked as answer – AndroidGuy007 Oct 17 '16 at 16:28
  • Thanks! I've just updated it to work exactly how you wanted to. :) – Mihály Lestyán Oct 17 '16 at 16:29
  • hello i have question can we use for loop something like this for(int i = 0;i < names.length;i++) { namesDescriptions = new NameDescription(names[i], desc[i]); } because this giving me errors i have fixed error new error comes up – AndroidGuy007 Oct 17 '16 at 17:01
  • For me it's working. There might be some other problem on your side. Please update your question so we can see the actual problem. – Mihály Lestyán Oct 17 '16 at 17:32
  • i am putting those values in another string array so i added – AndroidGuy007 Oct 17 '16 at 17:38
  • for(int i = namesDescriptions.length-1; i >= 0; i--) { for(int j = 0; j < namesDescriptions.length; j++) { names[j] = namesDescriptions[i].getName(); desc[j] = namesDescriptions[i].getDescription(); imageid[j] = namesDescriptions[i].getimg(); break; } } – AndroidGuy007 Oct 17 '16 at 17:38
  • well i fixed it – AndroidGuy007 Oct 17 '16 at 17:40
  • int j = 0; for(int i = namesDescriptions.length-1; i >= 0; i--) { names[j] = namesDescriptions[i].getName(); desc[j] = namesDescriptions[i].getDescription(); imageid[j] = namesDescriptions[i].getimg(); j++; } – AndroidGuy007 Oct 17 '16 at 17:40
  • your works too ,well put j int and j++ in for loop works fine,anyway i appreciate your work,did we initialize namedescription with for loop i dont want to put values again and again ,if we can get names.length and do for loop for namesdescription ,, something like this NameDescription namesDescriptions[] = { new NameDescription(names[i], desc[i]), this way it does for loop and add itself rather than putting it one by one for every single input like you did above – AndroidGuy007 Oct 17 '16 at 18:03