I have a List<String> and a List<Integer>. Both are in specific order(they are linked). List<String> contains names and List<Integer> their values.
Is there a way to sort List<Integer> by size but also change ordering of List<String> so that values and names stays linked?
You should use a
List<NameAndValue>instead of two lists (and find a better name than NameAndValue, which would reflect what these data actually represent). Java is an OO language. Use objects. That’s what they’re for: containing data that are related, and providing behaviour with methods and encapsulation.Once you have this class, you’ll be able to sort your list by name, value or both, and adding an additional field if needed won’t be a problem.