'Arraylist of Objects not getting sorted or swaped
I want to sort the objects, and I have applied the approach two ways, however none works. When printing the the content of the arraylist, the elements are in the order they were inserted, and not lexicographically sorted.
I did do research within stackoverlow:
- Sort ArrayList of ArrayList of objects
- Sorting an ArrayList of objects (ArrayList not being changed after sorting)
I did look into the implements approach where we make override compareTo function. But, those approach are not making sense to me. I wanted to make my ArrayList a private variable and all the examples I saw had the ArrayList in main class (I am new to OOP langauges too, and wanting to learn if someone is willing to give appropriate resources).
Any help or hint is appreciated.
Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int puedecCapacity = 0;
String spa_nameString = null;
Puedec puep = new Puedec();
System.out.print("Enter 1st string to add: ");
spa_nameString = scnr.next();
puep.addPue(spa_nameString);
System.out.print("Enter 2nd string to add: ");
spa_nameString = scnr.next();
puep.addPue(spa_nameString);
System.out.print("Enter 3rd string to add: ");
spa_nameString = scnr.next();
puep.addPue(spa_nameString);
puep.sortPue();
puep.printPue();
}
}
Puedec.java
import java.lang.*;
import java.util.*;
public class Puedec {
private int Puedec_size;
private int Puedec_size_fill_count;
private ArrayList<Pue> pue_ArrayList = new ArrayList<Pue>(getCapacity());
public Puedec() {
this(3);
}
public Puedec(int capacity) {
Puedec_size = capacity;
}
public int getSize() {
return Puedec_size_fill_count;
}
public int getCapacity() {
return Puedec_size;
}
public boolean addPue(String spa) {
if(getSize() == getCapacity()) {
System.out.println("Error");
return false;
}else {
for(Pue p : pue_ArrayList) {
if(p.getSpa().contains(spa)) {
System.out.println("Error");
return false;
}
}
pue_ArrayList.add(new Pue(spa));
++Puedec_size_fill_count;
return true;
}
}
public void sortPue() {
// This also not changing anything in the ArrayList
/*
pue_ArrayList.sort((pk1, pk2)
-> pk1.getSpa().compareTo(
pk2.getSpa())
);
*/
int size = pue_ArrayList.size();
for(int i = 0; i < pue_ArrayList.size() - 1; i++) {
for(int j = 0; j < pue_ArrayList.size(); j++) {
if(pue_ArrayList.get(i).getSpa().compareTo(pue_ArrayList.get(j).getSpa()) > 0) {
Pue tempPue = pue_ArrayList.get(i);
pue_ArrayList.set(i, pue_ArrayList.get(j));
pue_ArrayList.set(i, tempPue);
}
}
}
}
public void printPue(){
for(Pue pp : pue_ArrayList) {
System.out.println(pp.getSpa());
}
}
}
Pue.java
public class Pue {
private String spa;
public Pue(String spa) {
this.spa = spa;
}
public String getSpa() {
return spa;
}
}
Solution 1:[1]
You can try adding this code to your sortPue() method:
public void sortPue() {
Collections.sort(pue_ArrayList,
new Comparator<Pue>()
{
public int compare(Pue pp1, Pue pp2)
{
return pp1.getSpa().compareTo(pp2.getSpa());
}
});
}
When implementing Comparator, you have to specify the types you want to compare, in this case there is <Pue>. Then, we have to add an implementation for the compare method, passing two object references to be compared. In the end, String's compareTo method is used to sort the list.
Collections.sort takes 2 parameters:
- the list to be sorted;
- the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
String compareTo returns:
- the value
0if the argument string is equal to this string; - a value
less than 0if this string is lexicographically less than the string argument; - a value
greater than 0if this string is lexicographically greater than the string argument.
Solution 2:[2]
I have two solutions in mind.
Since you said that when we override compareTo and etc. didn't make so much sense to you, I'll provide a really basic solution aswell after the most simple one.
The most simple
public void sortPue() {
pue_ArrayList.sort(Comparator.comparing(Pue::getSpa));
}
The basic one
List<Pue> listToSort = puep.getPue_ArrayList();
Pue[] array = listToSort.toArray(new Pue[0]);
System.out.println("before sorting");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i].getSpa());
}
System.out.println("after sorting");
for(int i = 0; i < array.length-1; ++i) {
for (int j = i + 1; j < array.length; ++j) {
if (array[i].getSpa().compareTo(array[j].getSpa()) > 0) {
Pue temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("\nAfter performing lexicographical order: ");
for(int i = 0; i < array.length; i++) {
System.out.println(array[i].getSpa());
}
Output:
In case it works and you want an explanation on anything, I can try to explain it in details! I hope it helps.
Solution 3:[3]
At a quick glance, this is probably the source of the problem.
Pue tempPue = pue_ArrayList.get(i);
pue_ArrayList.set(i, pue_ArrayList.get(j));
pue_ArrayList.set(i, tempPue);
So you take out the i'th element, set the i'th element to something else, then set the i'th element back to what it was. Net result: nothing changed.
Likely that last use of 'i' should be 'j', but I did not analyze this in any detail.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AOE_player |
| Solution 2 | Pedro Luiz |
| Solution 3 | passer-by |

