0

Ive got 2 Array lists

 ArrayList<String> passengerNames= new ArrayList<String>();//stores passenger names
ArrayList<String> seatNumber= new ArrayList<String>();//stores the seat passenger booked 1  seat=1passenger
    passengerNames.add("Ram");
    passengerNames.add("Shyam");
    passengerNames.add("CPS");
    passengerNames.add("John");
    passengerNames.add("Steve");

    seatNumber.add(1);
    seatNumber.add(84);
    seatNumber.add(19);
    seatNumber.add(99);
    seatNumber.add(78);

so the passengerNames array will be [Ram, Shyam, CPS, John, Steve] and seatNumber array will be [1, 84, 19, 99, 78] I want to sort the seatNumber array in accending order but I want to change the passengerNames list with it as in when seatNumber is in accending order like [1,19,78,84,99] the passengerNames array should be sorted like [Ram,CPS,Steve,Shyam,Jhon]

2
  • 1
    what is your question? "How to do it ?" Commented Apr 6, 2020 at 9:54
  • [Mod] - Please edit your question for a clearer more specific one, if needed please add relevant code samples Commented Apr 6, 2020 at 10:17

3 Answers 3

3

I would suggest encapsulating your data in a new Passenger object and creating a single ArrayList of type Passenger - this should keep things tidy and flexible.

public class Passenger {
    private String name;
    private int seatNumber;

    public Passenger(String name, int seatNumber) {
        this.name = name;
        this.seatNumber = seatNumber;
    }
}

... and for creating the Passenger ArrayList

List<Passenger> passengers = new ArrayList<Passenger>(); // build Passenger ArrayList
passengers.add(new Passenger("Ram", 1)); // create new Passenger
passengers.add(new Passenger("Shyam", 84)); // etc...

In regards to sorting the List in ascending order, a clean approach is to implement Comparable in the Passenger class, and define the behavior in the compareTo(Passenger p) method. Once that is defined to your liking, the sort can be called via Collections.sort(passengers);

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

Comments

1

Instead of having two arrays of information that belong to a single entity, create an Object Passenger in which you assign the name and seat number.

public class Passenger {

    private String name;
    private int seatNumber;

    public Passenger(String name, int seatNumber){
        this.name = name;
        this.seatNumber = seatNumber;
    }
    // setters and getters
}

Now just create your array of passengers

ArrayList<Passenger> passengers = new ArrayList<Passenger>();
passengers.add(new Passenger("Ram", 1));

Then use a sorting method by Object attribute.

Arrays.sort(passengers, Comparator.comparing(Passenger::getSeatNumber));

Comments

0

Looks like it would be more appropriate to use a SortedMap

SortedMap<Integer, String> map = new TreeMap<>();
map.put(1, "Ram");
map.put(84, "Shyam");
map.put(19, "CPS");
map.put(99, "John");
map.put(78, "Steve");

You don't need to pass a Comparator in the constructor in your case, because you need the keys to be in ascendent order, that is their natural order. The cool thing is that they will be already sorted without doing anything, they will sort automatically as you add them

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.