5

I am working on an application that saves information about restaurants, each restaurant has 5 info " as listed in the code below ", I need to sort the restaurants according to the rate. However, after I sort according to rate I need to synchronize all the array lists together so that their information fits correctly.

I have 5 ArrayLists, I need to sort one of them, but I also want to change the indexes of the 4 other ArrayLists so they can fit with each other. I tried the Map list but it tells me that i can do it with only 2 parameters and not 5.

Here is my ArrayLists :

//******* Arrays that holds information of restaurants taken from SERVER
static public ArrayList<String> name = new ArrayList<String>();
static public ArrayList<String> lng = new ArrayList<String>();
static public ArrayList<String> lat = new ArrayList<String>();
static public ArrayList<String> address = new ArrayList<String>();
static public ArrayList<String> rate = new ArrayList<String>();
static public ArrayList<String> comment = new ArrayList<String>();
//private Map<String, String> sorted = new HashMap<>(); Doesn't work with 5 parameters

I need to sort the rate List, but also change the indexes of the others.

6
  • 1
    Create a model containing all your required fields, create a List of your model object, sort the list. Commented Jun 26, 2015 at 22:16
  • It's really not at all clear what you are trying to do. Plus people will look kindly on an attempt to actually solve the problem. Commented Jun 26, 2015 at 22:16
  • 1
    @karaokyo I was just about to say that :) Then use Collections.sort and override your Comparator. Commented Jun 26, 2015 at 22:17
  • 1
    @RobertMoskal I edited the question that i believe it's more clear right now. Commented Jun 26, 2015 at 22:21
  • @karaokyo Do you mean that i just create a list that contains all 5 array lists? and then just sort one of the array lists? Commented Jun 26, 2015 at 22:22

1 Answer 1

5

You could create an objet Restaurant containing your various string attributs. This way you would be able to have only one list and call sort on it to sort all your informations at once.

You will have to implement Comparable in this Restaurant class to define how your restaurants should be sorted.

Edit :

Your Restaurant class would look like this:

public class Restaurant implements Comparable<Restaurant>{

    private String name;
    private String lng;
    private String lat;
    private String address;
    private String rate;
    private String comment;

    ...
    constructor/getters/setters
    ...

    @Override
    public int compareTo(Restaurant rest) {
        //TODO: check if rate is null if this can happen in your code
        return rate.compareTo(rest.rate); //comparing on rate attribute
    }

}

Then you can replace your code with

static public ArrayList<Restaurant> restaurants = new ArrayList<Restaurant>();
Collections.sort(restaurants);
Sign up to request clarification or add additional context in comments.

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.