
net.anotheria.util.sorter.BubbleSorter Maven / Gradle / Ivy
package net.anotheria.util.sorter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* An implementation of a bubble sorter for EQS FI Sorter Pattern.
*
* @author another
* @version $Id: $Id
*/
public class BubbleSorter extends AbstractSorter {
/** {@inheritDoc} */
public List sort(Enumeration source, SortType method){
List toSort = new ArrayList<>();
while(source.hasMoreElements())
toSort.add(source.nextElement());
return sort(toSort, method);
}
/** {@inheritDoc} */
public List sort(List source, SortType method){
boolean sortOrder = method.getSortOrder();
int sortAfter = method.getSortBy();
if (source==null || source.isEmpty())
return source;
T[] data = list2array(source);
boolean wanted = sortOrder == SortType.ASC ;//? true : false;
int l = Array.getLength(data);
boolean changed;
do{
changed = false;
for (int i=0; i0 :data[i].compareTo(data[i+1], sortAfter)<0 ){
swap(data, i, i+1);
changed = true;
}
}
}while(changed);
return array2list(data);
}
private static void swap(Object[] data, int first, int second){
Object tmp = data[first];
data[first] = data[second];
data[second] = tmp;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy