Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting list by value

Tags:

java

list

sorting

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?

like image 809
pedja Avatar asked Nov 23 '12 10:11

pedja


People also ask

How do you sort a list numerically in Python?

Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.

Can we sort list in Python?

The sort() method is one of the ways you can sort a list in Python. When using sort() , you sort a list in-place. This means that the original list is directly modified. Specifially, the original order of elements is altered.

How do you sort a value in Python?

To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.

What is sort () in Python?

Python sorted() Function The sorted() function returns a sorted list of the specified iterable object. You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically. Note: You cannot sort a list that contains BOTH string values AND numeric values.


1 Answers

You need java.util.TreeMap<Integer,String> . This sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

like image 84
Subhrajyoti Majumder Avatar answered Sep 29 '22 19:09

Subhrajyoti Majumder