#/help

15 messages · Page 1 of 1 (latest)

elder fractal
#

I have an array of Pairs.
The first value in the Pair represents coefficient of a polynomial, and the second represents the power of the polynomial.
How can I sort it in a descending order without creating a long code?

Someone here recommended me to use Collections.sort() on the array of Pairs, but I don't understand how it's possible to do this on this array, since it doesn't represent a list, so it's impossible to sort it.

I obviously need to sort it by the powers value, but how it's possible to do this and change the coefficient's indices in accordance with the power parameter?

Thank you!

cyan elbowBOT
#

This post has been reserved for your question.

Hey @elder fractal! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

velvet gorge
elder fractal
#

Yes exactly
I'm trying for so long to find a solution but didn't find anything about it

velvet gorge
#

Alright you are trying to sort the list by the powers in descending order correct ?

elder fractal
#

I already put inside the Pairts the 2 different arrays, now everything is mixed up and I need to sort them

#

Exactly

#

Pairs = [[7,3], [5,4], [8,6], [9,2]]
it looks something like this
I need to sort it to be like this:
Pairs = [[8,6], [5,4], [7,3 ], [9,2]]

velvet gorge
#

Ok lets say you have an unsorted arraylist that contains these pairs:
To sort that list you would use:
Collections.sort(list,Comparator.comparingInt(Pair::getValue).reversed());

elder fractal
#

something like this?
_polynomial is the ArrayList of Pairs

velvet gorge
#

Yeah but I seem to have gotten the syntax wrong. What is it complaining about ?

velvet gorge
# elder fractal something like this? _polynomial is the ArrayList of Pairs

Oh and I just noticed that you are using the Pair class found in the javafx.util package. If you are not developing a gui application with javafx I would use the "pair" class found in the standard library, namely Map.Entry since most people would use it in this context.
The code changes to:
Collections.sort(_polynomial,Comparator.<Map.Entry<Double,Integer>>comparingInt(Map.Entry::getValue()).reversed());

elder fractal
#

I'm sorry, I wasn't home for few hours, I'll save it and try to finish it tomorrow.
I'll sit the whole day tomorrow to do this

Thank you very much ❤️

cyan elbowBOT