Sort 1 « ArrayList « Java Collection Q&A

Home
Java Collection Q&A
1.algorithm
2.array
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
10.ArrayList
11.collection
12.comparator
13.Development
14.Garbage Collection
15.Generic
16.hash
17.HashMap
18.HashTable
19.iterator
20.LinkedList
21.List
22.Map
23.queue
24.Set
25.Sort
26.tree
Java Collection Q&A » ArrayList » Sort 1 

1. Sort ArrayList    stackoverflow.com

Here is a simple sorting program of an ArrayList:

ArrayList<String> list = new ArrayList<String>();

list.add("1_Update");
list.add("11_Add");
list.add("12_Delete");
list.add("2_Create");

Collections.sort(list);
for (String str : list) {
  System.out.println(str.toString());
}
I was expecting the output of this program as:
1_Update
2_Create
11_Add
12_Delete
But when I run ...

2. Java 2D ArrayList and sorting    stackoverflow.com

I need to sort a shopping list by the aisle the item is located for example:
[Bread] [1]
[Milk] [2]
[Cereal] [3] I am planning to do this with ArrayList and was ...

3. Array List Sorting    stackoverflow.com

How would you sort an array list alphabetically or numerically? I am not really sure how a method sorting an array either alphabetically or numerically would work. Thanks!

4. Sorting an arraylist of objects by date using SS::MM::HH MM/DD/YY/ format    stackoverflow.com

I have an arraylist of objects, each containing a unique date in the following format

YEAR-DAY-MONTH HOUR:MINUTE:SECOND (example: 2010-02-10 23:32:14)
I'm trying to compare each object in the array list (there are ...

5. Easiest Way to Sort a List of Words by Occurance    stackoverflow.com

What is the best/easiest way to sort a large list of words (10,000-20,000) by the number of times they occur in the list, in Java. I tried a basic implementation but ...

6. Sorting an arraylist in Java language    stackoverflow.com

I have an arraylist set up. I have input instuctions set up too, so that the user can enter one string, then one integer, then one string (the first name, the ...

7. Sort ArrayList alphabetically    stackoverflow.com

I'm trying to find all permutations of a string and sort them alphabetically. This is what I have so far:

public class permutations {

        public static ...

8. How to sort an arraylist of objects by a property?    stackoverflow.com

Lets say you have an arraylist of HockeyPlayer objects. How could you sort that if they all have a variable "int goalsScored". How could you sort them by goalsScored? ...

9. Using selection sort in java to sort floats    stackoverflow.com

Hey, I need to sort an array list of class house by a float field in a certain range. This is my code for the selection sort: Basically sortPrice copies the stuff ...

10. Sort ArrayList of custom Objects by property    stackoverflow.com

I had a question which is pretty easy if you know the answer I guess. I read about sorting ArrayLists using a Comparator but somehow in all of the examples people ...

11. Why 10 in the beginning of an ArrayList after Sort()    stackoverflow.com

As the question says I have this problem. I know why it is in the beginning, because it is a 1 and a 0, so therefore its before 9 even after ...

12. How to sort the array list?    stackoverflow.com

Hey Guys. thanx for the major help regarding my obstacles. What my problem this time is, how can I sort the array list that is provided in my code basically dont know ...

13. Keeping enums sorted in an arraylist?    stackoverflow.com

Say I had a an enum called Planets that contained VENUS, EARTH, and MARS. I will have a lot of array lists that will hold at most one of each type. ...

14. Sorted array list in Java    stackoverflow.com

I'm baffled that I can't find a quick answer to this. I'm essentially looking for a datastructure in Java which implements the java.util.List interface, but which stores its members in ...

15. Java: Sort an ArrayList based on an object field    stackoverflow.com

Possible Duplicate:
Sorting an ArrayList of Contacts
I am storing DataNode objects in an ArrayList. The DataNode class has an integer field called degree. I want to ...

16. Java how to sort an ArrayList of Point objects    stackoverflow.com

I'm using the Point Class to manage a list of (x,y) coordinates and I need to sort them in order of X. I read online to make a new class PointCompare that ...

17. sorting an arrayList    stackoverflow.com

Hi I have an arrayList which has some objects.also my objects has two fields (1) name (2) cost I want to sort this arrayList with its cost field.is there any special method that ...

18. Sort an ArrayList base on multiple attributes    stackoverflow.com

I have an ArrayList of object. The object contain attributes date and value. So I want to sort the objects on the date, and for all objects in the same date ...

19. Sorting ArrayList - IndexOutOfBoundsException -Java    stackoverflow.com

I'm trying to sort an ArrayList with strings(PlayersNames) and imageIcons(PlayersIcons) based on the values i store in an other arrayList with integers(results). As you can see i get an indexOutOfBoundsException but ...

20. sorting arrayLists    stackoverflow.com

I have a scenario where I have two ArrayLists

ArrayList<String> sortedArrayList
ArrayList<String> unSortedArrayList
I have to sort unSortedArrayList depending on the sortedArrayList. i.e, sortedArrayList is already sorted, now based on sortedArrayList, I have to sort ...

21. Sorting a part of Java ArrayList    stackoverflow.com

What is the most efficient way of sorting only a part of ArrayList? Say all elements from index 0 to 3 in an Arraylist which contains 10 elements. Is there a library ...

22. Sorting of ArrayList    stackoverflow.com

I have one array list

ArrayList itemListWithRank = ItemListDAO.getItemList();
and in arraylist itemListWithRank there are lots of type of objects values those all are different. And one value from them is item ...

23. Sorting of ArrayLIst    stackoverflow.com

How can i sort ArrayList in java without using comparator.? I have already used comparator and it work not properly for me, means data is not in particuler manner. I have one ...

24. sorting and searching an arraylist    stackoverflow.com

I've looked at the posts on here to do with sorting and searching but can't seem to get my arraylist to sort or search using

Collections.sort(myarraylist);
The data that is stored in the ...

25. How to sort a list of objects according to two paramaters to compare at Java?    stackoverflow.com

I have a class like that:

public class Zern extends Something{
 private int costA;
 private int costB;

 public int getcostA() {
     return costA;
 }

 public void setcostA(int costA) ...

26. Custom sorting of list based on date    stackoverflow.com

I got:

List<Comment> list = new ArrayList<Comment>();
My class Comment contains: id, name and date; I got some function that adds specified Comments, but I would like to order after that my list by ...

27. arraylist sorting    stackoverflow.com

i have two arraylist one have employee entity list other also have same entity list. one Arraylist have all employees and other one have selected employees. now i want to arrange employee in ...

28. Sorting an ArrayList of Objects by Last name and firstname in Java    stackoverflow.com

I have an arrayList of different types of players based on sports. I need to sort the list of players in the arrayList by last name to start. If ...

29. Sorting over multiple fields    stackoverflow.com

I am trying to sort first by car make then sort by car year then sort by car mileage. All the fields are strings. Here's what I've tried so far:

class Sorter implements ...

30. Selection sort - arrayLists    stackoverflow.com

Basically, I have data of several people contained in a .csv file. This spreadsheet file contains the name of people in one column, followed by their age and work experience in ...

31. junit tests : sorting ArrayList data within table    stackoverflow.com

I have a TreeTable and would like to perform the sort by number and alphabetically when clicking on header.

Example:

  • On a first click, I have to check that the column content is ...

32. Java: sorting an ArrayList in place    stackoverflow.com

In Java's standard library, is there a method that would allow one to sort an ArrayList in place, i.e. using O(1) extra storage? Collections.sort(List<T>) does not fulfil this requirement since ...

33. Sort an arraylist of objects in java    stackoverflow.com

I'm working on creating a simulating of some different CPU scheduling algorithms and also wanted to learn a new language at the same time. The trouble I'm running into is ...

34. java sort array list using bubblesort    stackoverflow.com

I have been working on this for the past 5 hours. for some reason I am not getting the result I am looking for. The method is supposed to sort an ...

36. Multi Level Sorting of a ArrayList    coderanch.com

Hi, I have a ArrayList containing the values retreived from the DB. I need to sort the data in two levels. The primary level is the field containing the Date and if the sorted values have the same dates the data needs to be sorted with the field containing Names. How do I do it? I have used the comparator to ...

37. Sorting two ArrayLists, one dependent on the other    coderanch.com

Hello all, I have two ArrayLists in my program: - one contains words, - and the other contains their respective pronunciation symbols. I want to sort the two arraylists The simplest way to sort the words is Collections.sort(WordsList). The problem is that i want to sort the pronunciations list as well, but not alphabetically, because its dependent on the words list. ...

38. ArrayList Sort    coderanch.com

39. How to sort an ArrayList of ArrayLists    coderanch.com

40. Sorting arraylist of objects    coderanch.com

I have to sort an arraylist of VO as per the following requirement.My VO looks like this. public class listBaseVO extends BaseVO implements java.io.Serializable { String bus_unit_cd; String strName; String role_cd; String rcv_stts_cd; //Get Set Methods } I want to sort the arraylist first with bus_unit_cd (By alphabetical order) and then by role code for each of this bus_unit_cd. This is ...

41. sorting an ArrayList Alphabetically    coderanch.com

42. Sorting ArrayList    coderanch.com

I have an ArrayList of data which looks as follows: "A" "H" "L1" "L100" "L11" "L2" "L3" "X" I would like to sort this ArrayList so that the numerical values are sorted correctly, such that the list looks like: "A" "H" "L1" "L2" "L3" "L11" "L100" "X" Any suggestions would be greatly appreciated. Stu...

43. Sort ArrayList based on date    coderanch.com

44. Sort ArrayList    coderanch.com

45. Sorting ArrayList of MemberBean objects    coderanch.com

Hi, I have an ArrayList which contains MemberBean objects. The MemberBean is displayed in the JSP as a table with four fields - First Name, Last Name, Member ID (integer) and User Name. I need to sort this based on user selected field and in either ascending or descending order. I know I can use the Comparator interface with the Collections.sort(List,Comparator) ...

46. Multi Level Sorting of a ArrayList    coderanch.com

Hi, I have a ArrayList containing the values retreived from the DB. I need to sort the data in two levels. The primary level is the field containing the Date and if the sorted values have the same dates the data needs to be sorted with the field containing Names. How do I do it? I have used the comparator to ...

47. comparing and sorting arraylist of arraylist    coderanch.com

Hello i would like some help for my problem. Here is an exemple of the arraylist of arraylist i have got : [[FB, 1, 0, 0, 0, 0, 0, 0, 1], [B, 1, 0, 0, 0, 0, 0, 0, 1], [BF, 2, 0, 0, 0, 0, 0, 0, 2]] My problem is that, from this arrayList, i have to compare the ...

48. sorting arraylist of arraylist    coderanch.com

Here's how I'd approach the comparator for a List>, considering null to be smaller than non-null: public int compare(List> list1, List> list2) { Iterator> i1 = list1.iterator(); Iterator> i2 = list2.iterator(); while (i1.hasNext() && i2.hasNext()) { List l1 = i1.next(); List l2 = i2.next(); int diff = compare(l1, l2); if (diff != 0) { return diff; } } // so far ...

49. sort an arraylist    coderanch.com

i would like to know, how can i sort an arraylist. My arraylist is composed of object with 3 attributes. And i wan't to sort this arraylist by one of this attribute. I don't know if i have to cast my arraylist in an other , and in which one. Thanks a lot.

50. Sorting an ArrayList of Object X    coderanch.com

You'll have to write your own Comparator for your own class. It doesn't implement a sort algorithm, tho. It's much simpler, thank goodness. It just has to compare two objects and tell which one should come first in sort order. If you're in JDK 1.5 try the "generics" feature to specify the type of object in your collection. It makes the ...

51. why is this arraylist sort not working?    coderanch.com

I'm working on a simple app to keep track of my cds. Development has been going well, but I'm running into problems with an arraylist sort - it sorts the cds alphabetically that fall under a specific letter. The problem - my sorting methods, whether I implement them in a Comparator.compare or compareTo don't seem to be called. Here's my Comparator ...

52. ArrayList sorting    coderanch.com

53. Sorting an Array List    coderanch.com

54. Sort Array List based on boolean    coderanch.com

55. ArrayList Sorting    coderanch.com

I've already customized Comparable to sort in two different ways (byMAC, byRSSI). The problem is sorting each group of sorted MAC by RSSI. Here is my idea for the next step: 1- After sorting by MAC, find the last index of the first MAC. 2- Partially sort the arraylist from (begining, indexOf -1). Now I can work on the partially sorted ...

56. ArrayList selection Sort    coderanch.com

57. Sorting ArrayList    coderanch.com

Please let me know how to sort the data in the below array list. import java.util.*; public class SortList { public static void main(String args[]){ ArrayList A1=new ArrayList(); ArrayList A2=new ArrayList(); ArrayList A3=new ArrayList(); ArrayList A5=new ArrayList(); A1.add("13");A1.add("America"); A2.add("123");A2.add("India"); A5.add("225");A5.add("Europe"); A3.add(A1); A3.add(A2); A3.add(A5); for(int i=0;i

58. [SOLVED] Sorting an ArrayList    coderanch.com

59. Sorting arraylist on the basis of object    coderanch.com

It can be achieved in two ways.. 1. implementing Comparable interface 2. implementing Comparator interface 1. DVDInfo Class is the one which has to be sorted say by genre.So it implements Comparable class DVDInfo implements Comparable{ String title; String genre; String leadActor; int id; DVDInfo(String t, String g, String l,int id){ this.id= id; this.genre=g; this.leadActor=l; this.title=t; } @Override public String toString() ...

60. Sorting an ArrayList is giving me fits    coderanch.com

This is my first post on this board. I am a java beginner, however, I have a graduate level class right now that requires a functional knowledge system that is written in java. Halfway through the class we started looking at Java and it's way more complicated than I expected. I am no stranger to programming (Basic, Pascal, Fortran, Matlab, VBA ...

61. Help me in using a Comparable with ArrayList for Sorting purpose    coderanch.com

Hi , I have defined a DTO of a Employee with name , age and other properties . On to the EMployeeDTO i have overriden the compareTo method as required for my business . Please tell me what should i do on to my DAO Layer of getEmployeeDetails() , so that i can display the results as per the Name order ...

62. Help with sorting ArrayList using Comparator    coderanch.com

public class Address implements Comparator

{ private int ID; private String firstName; private String surname; private String address; public Address(int inID, String inFirstName, String inSurname, String inAddress) { ID = inID; firstName = inFirstName; surname = inSurname; address = inAddress; } //.... other methods... public int compare(Address o1, Address o2) { String lastName1 = ((Address) o1).getSurname().toUpperCase(); String firstName1 = ((Address) o1).getFirstName().toUpperCase(); ...

64. Sorting Arraylist    coderanch.com

If the items contained in the ArrayList implement Comparable and if the compareTo method uses the age as the basis for comparison, you're set, simply call Collections.sort(yourArrayList) on your array list. If not, then you'll want to create a Comparator class that has a compare method that uses age as a basis for comparison and use an object of that class ...

65. Regarding sorting data in arraylist    coderanch.com

We really don't want people to hand out solutions. Many folk who ask questions are students. If solutions were handed out, it would be too easy for someone to do the unethical thing of copying the work of someone else and handing it in as their own. Further, the best way to learn is to do it yourself. Come back and ...

66. Sorting objects in ArrayList    coderanch.com

Collections.sort uses the Comparable interface to sort the object in the list, so these objects must implement Comparable and implement its method. The only method of that interface is int compareTo(T o). That is where you decide which fields of your objects have to be compared. Or, a Comparator can be used to compare the objects. In this case, Collections.sort(List list, ...

67. Sorting objects in an ArrayList    coderanch.com

68. Sort Arraylist by passing runtime parameter    coderanch.com

Shahid Pathan wrote:Thanks... I understood This will work surely if you want to sort by ageor salary or designation make three comparators as shown below /*Take a person class Attributes: name, age, salary A 1. Add few sample person objects to an array list 2. Sort arraylist based on persons age in descending order B 1. Sort arraylist based on persons ...

69. Sorting an ArrayList by a member of said MyObject    coderanch.com

Mmm the easiest way is use Collections utility and one of two: implements Comparable it that sorting is the natural sorting of you object and use Collections.sort(List) write a custom Comparator and use Collections.sort(List,Comparator) A comparator could be something like that (Date is already comparable ^__^) public class MyObjectDateComparator extends Comparator { public int compare( MyObject o1, MyObject o2 ) { ...

70. Sorting an ArrayList    java-forums.org

Hey, guys. I have a homework assignment due later tonight and am having a bit of trouble. I have to sort an array list as part of the assignment. My understanding is that must use Collections.sort, but I can't get that to work. I am not sure if that is due to my use of generics or what. Here is my ...

71. how to sort an arraylist?    java-forums.org

72. Sorting ArrayList by object data    java-forums.org

This is easy if the objects held by the ArrayList implement the Comparable interface. If so, simply call java.util.Arrays.sort(myArrayList), and pick out the top 10. If the objects don't implement Comparable, you'll could do the same but sort with a Comparator object. Google for a tutorial and give it a try. Much luck!

73. ArrayList sort    java-forums.org

74. Java - Comparator, sorting arrayLists    java-forums.org

Hi So as you might guessed i am trying to sort my arrayList of scores. I have been instructed to look at the comparator and compareTo(). And i have really been trying, i tried to look at examples, guides and the API. But it just wont stick, i just cant figure out where to put the compareTo and how to implement ...

75. Array List Sorting    java-forums.org

77. sorting arraylist based on another arraylist    java-forums.org

how can i sort 2 arraylist but only using 1 of them as criteria. Like if I have arraylists and want to sort it based on the integers of x in ascending order. x: [3,5,1,7,4] y: [4,6,3,10,11] I need to sort it so the arraylists become x: [1,3,4,5,7] y: [3,4,11,6,10] so arraylist y is sorted according to x's ascending status, (meaning ...

78. Sort an ArrayList    java-forums.org

see Java Code: package javatalk; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortEx { private static class User { private String name; private String soname; private User(String name, String soname) { this.name = name; this.soname = soname; } public String getName() { return name; } public String getSoname() { return soname; } @Override public boolean equals(Object o) { ...

79. Checking if an ArrayList is sorted    java-forums.org

80. Why is my ArrayList not sorted?    java-forums.org

I am trying to use the compareTo method from one class, and sort the data from a file in an ArrayList, but when the program runs, the ArrayList is not sorted. The data in the file is: Siena,Catherine,4.0,AppliedMath Gateau,Jean,3.5,AppliedComputing Jones,Jill,3.0,AppliedMath Jones,Jack,2.5AppliedComputing The names must be sorted alphabetically according to last name, then first name, so that it prints this: Gateau,Jean,3.5,AppliedComputing Jones,Jack,2.5AppliedComputing ...

81. Sort an ArrayList of an ArrayList    java-forums.org

This morning I came across this following code from an old post to this forum: Original thread can be found here: adding values from file to multi-dimensional ArrayList ArrayList> data = new ArrayList>(); data.add(new ArrayList()); data.get(0).add("String"); This code does exactly what I want; a two dimensional array. Looks something like this: [0][0] = ABC [0][1] = ABC [0][2] = AJ ABC ...

82. Sort ArrayList Month wise    forums.oracle.com

83. ArrayList Sorting....    forums.oracle.com

84. Can we sort an ArrayList using a Comparator ?    forums.oracle.com

85. Sorting an Arraylist    forums.oracle.com

hey i have any arraylist called list in which i have the information in the following format name jack chan phone 99887766 email j.e@dragon.com.au address null name jet li phone 88888888888888888 email j.l@dragon.com address null name bruce lee phone 1231231231231 email b.l@dragon.com address dead st and i attempt to sort it using the Collections.sort method eg Collections.sort(list, new AddressComparator()); my code ...

86. Searching and Sorting ArrayList objects    forums.oracle.com

public class Test { public static void DisplayArrayList(String name, ArrayList list) { String output = name + "\n"; for(int index=0; index CustList = new ArrayList(); Customer myCust = null;

87. Sort Parallel ArrayList    forums.oracle.com

Create a new array to store index values. int index[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; While sorting, swap values in index array only, and keep other arrays as it is. May be you need to write your own sort code, Collections.sort may not be useful here. After sorting, access values like myArray[index[0]], myArray[index[1]], myArray[index[2]] etc. ...

88. ArrayList Sort    forums.oracle.com

89. sorting data of an ArrayList    forums.oracle.com

90. Multiple sorting on ArrayList Object    forums.oracle.com

Hello all... I have a program that contains a database of all the hurricane data for the last fifty years or so. In short, my program is structured as so: A StormLibrary, which contains an ArrayList of Storm objects. Each Storm object contains an ArrayList of DataSamples. Each DataSample object contains several variables, all primitive, such as windspeed, latitude, longitude, etc., ...

91. Sorting an arraylist with 'Comparator'    forums.oracle.com

Hi guys, I've racked my brains trying to solve this one and trying to understand how the Comparator interface works. What I'd like to do is sort a arraylist by it's values.For example: John,8 David,2 Peter,7 Joe,3 Jason,5 Mike,2 to be sorted as: David,2 Mike,2 Joe,3 Jason,5 Peter,7 John,8 Any assistance or hints would be most appreciated.Especially pointers on the ...

92. Attempting to sort an ArrayList    forums.oracle.com

Hello I've somehow managed to fix it. I don't know how. I added some NorTourLocation objects to the ArrayList, and suddenly it worked. I took the added ones away (leaving me with the original number) and it still worked. I can only assume it might have had something to do with it being part of a web application which might have ...

93. Sorting an ArrayList    forums.oracle.com

94. need help with sorting an ArrayList    forums.oracle.com

But that's my point. Most of comparator examples (like the one in the article you provided) are for comparing fields like strings: the public class CaseInsensitiveComparator implements java.util.Comparator { public int compare(Object o1, Object o2) { String s1 = o1.toString().toUpperCase(); String s2 = o2.toString().toUpperCase(); return s1.compareTo(s2); } } I'm guessing an Integer comparator wouldn't use String objects, compareTo, etc. That's where ...

95. sorting ArrayLists    forums.oracle.com

96. Sorting a arrayList    forums.oracle.com

97. sorting array list    forums.oracle.com

98. sorting arraylists    forums.oracle.com

Hello all, I have an arraylist that holds a Password object. The password object contains a description, userid and password. I am wanting to sort my arraylist based off the Password objects descriptions alphabetically. I have looked at Collections.sort(arraylist) but that requires the Password class be comparable. I am having trouble setting up the compare to method from the comparable class. ...

99. Problems with sort, search and write objects o an ArrayList    forums.oracle.com

Hi Lets say that i have two subclasses (the program is not finished so in the end it can be up to 34 classes) of an abstract superclass. I also got one class which basicly is a register in which i've created an ArrayList of the type . This means that i store the two subclasses in the arrayList. no problems ...

100. sorting an arraylist    forums.oracle.com

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.