# Tag: sorting

## How to Peek the Lowest elements from a List of sorted Streams continously

I started learning about Java Stream, and I am wondering is it possible to only peek the first element of the stream without retrieving it. For example, I have multiple streams, and each of them have integers that are sorted in non-decreasing order, and I want to get a sorted list of all integers, so I’m thinking about using a

## Sort 2d String array by integer in first column

I have a 2d array in Java 8: String data[][] = new String[i]; And it looks like this: Player1 8 Player5 3 Player3 9 Player4 5 … and I want to sort it to have the highest score at the top: Player3 9 Player1 8 Player4 5 Player5 3 How can I sort this or is there a better way

## Java sorting list of array vs sorting list of list

I have a list of points where each point is a tiny list of size 2. I want to sort the list of points in increasing order of x and if x values are equal, I break tie by sorting in decreasing order of y. I wrote a custom comparator to sort the points like this: Here’s the input before

## Java Quick Sort Performance

I was doing sorting array problems and found one of the quick sorting solution extremely fast, and the only difference is the two lines of code in function 1Partition. Wondering why the following two lines of code in 1Partition can greatly improve the performance: Here’s the full source code: } Answer I guess you are doing the question on a

## create a sub list of elements based on user input

I wrote the code below, where from an initialized list of countries the method will return all the countries who have a longitude of >= 5, the code works but now I want the method to return the name of countries that are close to String D within a 5 degrees range of longitude. I tried implementing the scanner as

## How to sort the hashmap in descending order by values and if the values are the same then by key in ascending order

I have a HashMap<Integer, Integer> named “relevance” e.g {2: 3, 1: 3, 3: 3, 5: 4, 4: 4, 6: 3} and I want do DESC sorting by values. I get a hashmap {5: 4, 4: 4, 2: 3, 1: 3, 3: 3, 6: 3}. How can I sort ascending keys with the same values? Answer You can “chain” Comparators by

## Sorting LinkedHashMap<String, ArrayList> by counting the occurrences in ArrayList problem

I have: How can i sort “maps” by counting the occurrences in “miss”? For example: miss => [3, 7] maps => {1=[0, 3, 6], 4=[2, 3, 4], 6=[0, 3, 7], 11=[1, 3, 6], 17=[2, 6, 11]} And i want to get: maps => {6=[0, 3, 7], 1=[0, 3, 6], 4=[2, 3, 4], 11=[1, 3, 6], 17=[2, 6, 11]} Answer The

## Difficulty trying to sort 10 numbers inputted by a user. Must use arrays and a separate method for sorting

My program isn’t sorting the numbers at all. It displays them in the order they were initially entered. It must sort them from smallest to largest number. The code below should find the largest number in the array and swap it with the last .the code is below: Answer I believe your problem is here: array.length is not less than

## Sort List of Values in a Map in descending order

I have a Map with List of Doubles. unsorted map contents: {A=[0.02, 0.03], B=[0.0, 0.01], C=[0.01, 0.0], D=[0.05, 1.03], E=[1.01, 0.03]} I need to sort in descending order for List(0). I have tried the below logic but its sorting in ascending order. Actual output: {B=[0.0, 0.01], C=[0.01, 0.0], A=[0.02, 0.03], D=[0.05, 1.03], E=[1.01, 0.03]} Expected output: {E=[1.01, 0.03], D=[0.05, 1.03],

## JAVA – Sort Array of Json by multiple values while preserving previous sort order

I have this json and I want to be able to sort it by multiple field values of different types while retaining the previous sort order. The fields by which I should decide to sort it are given through another json: I managed to achieve this by converting the json array to a list of Hashmap<String,Object> and wrote a custom