Sort arrays of primitive types in descending order

0 votes

I got a huge array of double types. How can I sort elements in descending order?

The Java API does not support the primitive type (like double) sorrting with a comparator. First approach that I had was

double[] array = new double[1048576];    
Arrays.stream(array).boxed().sorted(Collections.reverseOrder())…

But, boxing each primitive is way too slow and creates a lot of GC pressure. Another approach that I cam up with is to sort first and then reverse it

double[] array = new double[1048576];
...
Arrays.sort(array);
// reverse the array
for (int i = 0; i < array.length / 2; i++) {
     // swap the elements
     double temp = array[i];
     array[i] = array[array.length - (i + 1)];
     array[array.length - (i + 1)] = temp;
}

This approach is also slow. What is a better way to solve this problem?

Aug 3, 2022 in Java by krishna
• 2,820 points
1,032 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,140 points
1,663 views
0 votes
2 answers

How to sort array in descending order

there are some traditional ways that you ...READ MORE

answered Jul 4, 2018 in Java by Priyaj
• 58,020 points
1,206 views
0 votes
2 answers

How can I sort values of a Map in Java using its key

Assuming TreeMap is not good for you ...READ MORE

answered Oct 10, 2018 in Java by Sushmita
• 6,920 points
1,230 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,220 points
3,052 views
0 votes
1 answer

Types of IOC containers in Spring Framework

There are basically two types of IOC ...READ MORE

answered Aug 28, 2018 in Java by misc.edu04
• 1,450 points
26,690 views
0 votes
1 answer

Types of transaction management in Spring Framework

Well there are basically to types of ...READ MORE

answered Nov 28, 2018 in Java by geek.erkami
• 2,680 points
1,937 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
880 views
0 votes
1 answer

Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,770 points
1,503 views
0 votes
0 answers

Java : Sort integer array without using Arrays.sort()

I have this assignment- Write java program ...READ MORE

Aug 2, 2022 in Python by krishna
• 2,820 points
2,175 views
0 votes
0 answers

Sort a single String in Java

Is there a primary way to sort a ...READ MORE

Sep 23, 2022 in Java-Script by Abhinaya
• 1,160 points
527 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP