package guru.springframework.blog.sortarraylist.ascendingdescending; import org.junit.Test; import java.util.ArrayList; import static org.junit.Assert.*; public class SortArrayListAscendingDescendingTest { @Test public void testSortAscendingDescending() throws Exception { ArrayList countryList = new ArrayL...
//Natural orderCollections.sort(arrayList);//Reverse orderCollections.sort(arrayList,Comparator.reverseOrder());//Custom orderCollections.sort(arrayList,Comparator.comparing(Task::name)); 4. SortArrayListusing Java 8 Streams Using Java streams provides the opportunity to apply other intermediate operations...
package guru.springframework.blog.sortarraylist.ascendingdescending; import org.junit.Test; import java.util.ArrayList; import static org.junit.Assert.*; public class SortArrayListAscendingDescendingTest { @Test public void testSortAscendingDescending() throws Exception { ArrayList countryList = new ArrayL...
SortArrayListAscendingDescendingTest.java package guru.springframework.blog.sortarraylist.ascendingdescending; import org.junit.Test; import java.util.ArrayList; import static org.junit.Assert.*; public class SortArrayListAscendingDescendingTest { @Test public void testSortAscendingDescending() throws Exception...
public ArrayList sortAscending(){ //升序排序方法 Collections.sort(this.arrayList); return this.arrayList; } public ArrayList sortDescending(){ //降序排序方法 Collections.sort(this.arrayList,Collections.reverseOrder()); return this.arrayList;
import java.util.ArrayList; import static org.junit.Assert.*; public class SortArrayListAscendingDescendingTest { @Test public void testSortAscendingDescending() throws Exception { ArrayList countryList = new ArrayList<>(); countryList.add("France"); countryList.add("USA"); countryList...
/* For Ascending order*/ return this.studentage-compareage; } @Override public String toString() { return "[ name=" + studentname + ", age=" + studentage + "]"; } } import java.util.ArrayList; import java.util.Collections;
在 sortAscending()方法中,我们调用了 Collections.sort()方法,并传递这个初始化的 ArrayList对象为参数,返回排序后的 ArrayList。在 sortDescending()方法中,我们调用重载的 Collections.sort()方法让其按照降序对元素排序,这个版本的 Collections.sort()接收ArrayList对象作为第一个参数,一个由 Collections.reverseOrder(...
/* For Ascending order*/ return this.studentage-compareage; } @Override public String toString() { return "[ name=" + studentname + ", age=" + studentage + "]"; } } import java.util.ArrayList; import java.util.Collections;
有几种方法可以对其进行排序。您可以在类中实现Comparable,也可以使用Comparator。你可以这样做:...