473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ArrayList Sorting Problem

freddieMaize
Hi Guys,

I have an arraylist named c like the below,
Expand|Select|Wrap|Line Numbers
  1. [654,6465,98798,321123,874,654645];
I sort it by converting it into a int array
Expand|Select|Wrap|Line Numbers
  1. Arrays.sort(c);
But then, i have a arraylist like below,
Expand|Select|Wrap|Line Numbers
  1. [google,5462,yahoo.2245,aol,8896]
The above array is something like, [name of the site,response time,name of the site,response time,...]

All i want to do is to sort the arrylist with respect to the response time and arrange the List accordingly.

A sample output am looking for is like below,
Expand|Select|Wrap|Line Numbers
  1. [yahoo,2245,google,5462,aol,8896]
Here yahoo is in the first element becasue its response time is the least

All i'm trying to do is to access different sites and find out its response time and finally display the sites with the least responding time as the first and then the next least and so on...

Kindly suggest me a way to sort this.. Thank you...

Regards,
Freddie
Aug 25 '08 #1
6 2058
JosAH
11,448 Expert 8TB
So you basically rip an entity apart (a search engine and its response time) and
stick the remnants in an ArrayList and assume that list to be clever enough to
know what you mean by those names and numbers? Computers are way too
stupid for that; design a proper little class for that entity instead and make it
Comparable so that it can be sorted (and you don't need arrays for it).

kind regards,

Jos
Aug 25 '08 #2
Nepomuk
3,112 Expert 2GB
...design a proper little class for that entity instead and make it
Comparable so that it can be sorted (and you don't need arrays for it).
You can then make an ArrayList of the comparable Objects if you wish and sort them with Collections.sort(). Of course, you're free to choose any other way of collecting and sorting the Objects too apart from an ArrayList.

Greetings,
Nepomuk
Aug 25 '08 #3
Thanks guys... i solved my problem...

Below is the code snippet that i used...

Expand|Select|Wrap|Line Numbers
  1. abean.setName(urlArray[i]);  //setting the name of the site
  2. abean.setValue(Long.valueOf(responseTime));  //setting its response time
  3. responseTimeList.add(abean);
  4.  
Expand|Select|Wrap|Line Numbers
  1. Collections.sort(responseTimeList, new Comparator() {
  2. public int compare(Object arg0, Object arg1) {
  3.                 ABean obj1 = (ABean) arg0;
  4.                 ABean obj2 = (ABean) arg1;
  5.           return obj1.getValue().compareTo(obj2.getValue());
  6.             }
  7.             });
  8.  
Thank you,
Freddie
Aug 26 '08 #4
JosAH
11,448 Expert 8TB
Yup, that's the way to do it or make your bean class implement the Comparable
interface so that you don't need that Comparator for the Collections.sort() method.
But that doesn't matter much; well done.

kind regards,

Jos
Aug 26 '08 #5
Yup, that's the way to do it or make your bean class implement the Comparable
interface so that you don't need that Comparator for the Collections.sort() method.
But that doesn't matter much; well done.

kind regards,

Jos
Oh yeah... Thank you JosAh...

I was also wondering how to do this without all these Comparable interface or the sort()... I thought of below,

Because I know that you have to sort based on values at "even" positions of array, and I can directly access values of an array based on index,

Implement any sorting algorithm (eg: bubble sort) comparing values at even positions and when I swap the values, I swap not only values at x,y positions, but also swap x-1,y-1 positions at the same time.


However this wont be good for any application oriented work thought... This may be useful for some Assigment works :D

Cheers,
Freddie
Aug 27 '08 #6
Nepomuk
3,112 Expert 2GB
I was also wondering how to do this without all these Comparable interface or the sort()... I thought of below,

Because I know that you have to sort based on values at "even" positions of array, and I can directly access values of an array based on index,

Implement any sorting algorithm (eg: bubble sort) comparing values at even positions and when I swap the values, I swap not only values at x,y positions, but also swap x-1,y-1 positions at the same time.
That should work, although it's not at all a Object orientated solution (while the one you're using now is). If you want to do it without Objects, you could of course also have a 2D array like this:
Expand|Select|Wrap|Line Numbers
  1. private String[][] myArray = new String[2][4]; // 4 Websites with both names and response times as Strings
  2. ...
  3. private void swap(int a, int b)
  4. {
  5.    String tmp = myArray[0][a];
  6.    myArray[0][a] = myArray[0][b];
  7.    myArray[0][b] = tmp;
  8.  
  9.    tmp = myArray[1][a];
  10.    myArray[1][a] = myArray[1][b];
  11.    myArray[1][b] = tmp;
  12. }
But if possible, you should always prefer using Objects, like you do now.

Greetings,
Nepomuk
Aug 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
4
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net...
2
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the...
16
by: stojilcoviz | last post by:
I've a datagrid whose datasource is an arraylist object. The arraylist holds many instances of a specific class. I've two questions about this: 1 - Is there a way by which I can obtain a...
0
by: Grant Wickman | last post by:
Our team has just fixed a really nasty problem that appears to be caused by an obscure bug in the sort method of Arraylist and daisy-chained webservice stubs. We've fixed the bug so I'll not...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
11
by: Cappamontana | last post by:
Hi, I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this? I've coded some example here... ...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.