We are given a 2D array of order N X M and a column number K ( 1<=K<=m). Our task is to sort the 2D array according to values in the Column K.
Input : If our 2D array is given as (Order 4X4) 39 27 11 42 10 93 91 90 54 78 56 89 24 64 20 65 Sorting it by values in column 3 Output : 39 27 11 42 24 64 20 65 54 78 56 89 10 93 91 90
The Idea is to use Arrays.sort in Java.
39 27 11 42 24 64 20 65 54 78 56 89 10 93 91 90
Time complexity : O(n Log n) where n is number of rows. Here assumption is that the sort() function uses a O(n Log n) sorting algorithm.
This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Sorting a Hashmap according to values
- Sorting a HashMap according to keys in Java
- Sorting collection of String and StringBuffer in Java
- Sorting in Java
- Row wise sorting in 2D array
- Check if a String starts with any of the given prefixes in Java
- Default array values in Java
- Assigning values to static final variables in Java
- Returning Multiple values in Java
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- Assigning long values carefully in Java to avoid overflow
- Map Values() Method in Java With Examples
- Stream.of(T... values) in Java with examples
- ChronoUnit values() method in Java with Examples
- Iterating over Enum Values in Java
- HashMap values() Method in Java
- TreeMap values() Method in Java
- EnumMap values() Method in Java
- WeakHashMap values() Method in Java
- IdentityHashMap values() Method in Java