0

I have this city class that i need sorted. Tried to find other posts but non actually helped because i could not use the Collections in the way that i wanted to use it. I have this array with integers that i want to sort, which is easy, but then i want to sort a string array in the same way as the integer array was sorted.

Tried to use collections but can only sort one array.

City(int i, String string) {
    ArrayList<Integer> zipCode = new ArrayList<Integer>(); // This array is the integer.
    zipCode.add(i);
    i = Z;
    this.i = i;
    ArrayList<String> cityName = new ArrayList<String>();
    cityName.add(string);
    string = c;
}

public void list(String c) {
    ArrayList<String> l = new ArrayList<String>(); // This is the string.
    l.add(c);
    // Here i want to be able to sort the string in the same order as the "zipCode"
    // array or integer array.
    for (String X : l) {
        System.out.println(X);
    }

}

Just expect to get the order correct, i can do everything else fine but it is the sorting that is bothering atm.

I am getting some strings of a city from my main class and some integers from the citys zip code, also from my main class. Somehow i want to be able to sort the whole array of string and int together by the values of the integers. But i don't see anyone else doing an this exact thing..

2
  • 1
    Your code does not make any sense, please provide a minimal reproducible example. What is Z and c in the constructor? If you define the ArrayLists in the constructor they are not available to the list method... Commented Jan 18, 2019 at 10:53
  • ArrayList uses internally array, but it isn't array itself. Please don't mix them. This is array String[] array = {"foo", "bar"} this is list based on array List<String> list = new ArrayList<>(); list.add("foo"); list.add("bar");. Commented Jan 18, 2019 at 10:54

2 Answers 2

3

There are many things need to be evaluated and fixed in the code.

First, zipCode, cityName and l aren't available outside their function. zipCode and cityName should be properties of the class. But that is still not what you want.

If I got you right, you want to handle ArrayList of Cities, which each city has ZipCode and CityName. You need to understand first how to define your classes. So, we just figured out a class called City that has two properties, lets define it:

public class City {

    private int zipCode;
    private String cityName;

    public City(int zipCode, String cityName) {
        this.zipCode=zipCode;
        this.cityName=cityName;
    }

    public int getZipCode() {
        return zipCode;
    }

    public String getCityName() {
        return cityName;
    }

}

Now, you need a class (maybe the main class) that will hold an ArrayList of Cities (ArrayList<City>) Notice that each item in that array is a City type variable - so it holds both the zipCode and the cityName, Now, when you sort that array (by zipCode) - the whole item (Cities) will be sorted:

 public static void main(String[] args){

 ArrayList<City> cities = new ArrayList<City>();


 cities.add(new City(999,"NINE"));
 cities.add(new City(123,"COOL"));
 cities.add(new City(456,"FUN"));


 for(int i=0;i<cities.size();i++) {
     System.out.println(cities.get(i).getZipCode() + cities.get(i).getCityName());
 }

 Collections.sort(cities, (c1,c2) -> c1.getZipCode()-c2.getZipCode());
 System.out.println();

 for(int i=0;i<cities.size();i++) {
     System.out.println(cities.get(i).getZipCode() + cities.get(i).getCityName());
 }

 } 

result:

999NINE
123COOL
456FUN

123COOL
456FUN
999NINE

How the sorting works?

Collections.sort(cities, (c1,c2) -> c1.getZipCode()-c2.getZipCode());

Sort function of Collections, gets the ArrayList as first argument, and the comparator (a function that explains HOW to compare the items) as a second argument.

Well, cities is your ArrayList (list of cities), and the comparator is a function the gets two cities (c1 and c2), and returns the subtraction of both zip codes (resulting 0 means they are equal, resulting negative means that c2 zip code is bigger, and resulting possitive means that c1 zip code is bigger).

Key takeouts:

  1. Take a moment to think about the class definitions - what properties every class has
  2. You don't want to define new ArrayList inside a contructor (in cases you want that list to "live" outside the function)
  3. Read more about ArrayList, Collections, Comparator, sorting.

Hope it helped :)

Sign up to request clarification or add additional context in comments.

Comments

0

Create one array of objects which contains 2 fields - int value ans String value, and sort this array

3 Comments

Tried that but i can try again. So i create a map with string and int and then sort the whole array by int?
No. anber means you should create a class that contains 2 fields, city and zipcode. Then put them into an array and sort it by a comparator. Why you don't create the minimal example as SBylemans said in the first place?
I delete the second field... i can keep my constructor and just make another field...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.