Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Java array list from a class

Status
Not open for further replies.

soniajain

Newbie
Newbie level 3
Joined
May 15, 2023
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
United States
Activity points
34
Hi everybody!

I need to sort an Array List from a class.
I did this with "Collections.sort(ArrayList);" tried.
Yet, that doesn't work since it is by all accounts some unacceptable data type.
Double values are stored in this array list.

How could you at any point respond there?
 

that doesn't work since it is by all accounts some unacceptable data type.
One would expect to see the code and the specific compiler error you are talking about, not the narrative about whatever you meant.
Anyway, are you trying to sort values from different types?
 

By default, the sort() function sorts the values in string order. This function works great for strings ("apple" will come before "banana").
However, if the numbers are sorted by string, "25" is greater than "100" because "2" is greater than "1". Therefore, the sort() method may produce incorrect results when sorting numeric values.

[MODERATOR ACTION]

Translated to English
 
Last edited by a moderator:
Hi everybody!

I need to sort an Array List from a class.
I did this with "Collections.sort(ArrayList);" tried.
Yet, that doesn't work since it is by all accounts some unacceptable data type.
Double values are stored in this array list.

How could you at any point respond there?
To sort an ArrayList of Double values in Java using Collections.sort, you should ensure that the ArrayList is of type Double (i.e., ArrayList<Double>) and not a raw ArrayList type. Here's the correct way to sort an ArrayList of Double values:

Java:
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        ArrayList<Double> doubleList = new ArrayList<>();

        // Add double values to the ArrayList
        doubleList.add(3.5);
        doubleList.add(1.2);
        doubleList.add(2.7);
        doubleList.add(0.8);

        // Sort the ArrayList
        Collections.sort(doubleList);

        // Print the sorted ArrayList
        for (Double value : doubleList) {
            System.out.println(value);
        }
    }
}

Make sure that your ArrayList is explicitly typed as ArrayList<Double>. This code will sort the ArrayList of Double values in ascending order. If you want to sort in descending order, you can use the Collections.reverse() method after sorting.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top