Solved

How do i sort 2 arraylist elements such as these?

Posted on 2009-04-14
14
290 Views
Last Modified: 2012-05-06
1st element : Hello World F 9
2nd element : Holla World M 8
3rd element: Holla holla G 7

I want to sort them based on the numerical values as well as on the 3rd char in the list. can anyone provide me with a working code for this?
0
Comment
Question by:ravivanjani
  • 7
  • 7
14 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 24140894
Try a Comparator with the following:
public int compare(Object o1, Object o2) {

	String[] a1 = ((String)o1).split(" +");

	String[] a2 = ((String)o2).split(" +");

	int result = a1[2].compareTo(a2[2]);

	if (result == 0) {

		result = Integer.parseInt(a1[3]) - Integer.parseInt(a2[3]);

	}

	return result;

}

Open in new window

0
 

Author Comment

by:ravivanjani
ID: 24140962
Ok the problem is..i dont understand what do i pass in object 1 and object 2 and i do not understand where the string array comesfrom. This is my code. and the actual contents of the file are as such.

Kournikova Anna F F 6-3-1975 Red
Hingis Martina M F 4-2-1979 Green
Seles Monica H F 12-2-1973 Black
package test;
 

import java.util.*;

import java.io.*;
 

public class Testcompare

{

	public List li = new ArrayList();

	public StringBuffer s = new StringBuffer();

	public String b;	

	public void readfiles()

	{

		try

		{	

			BufferedReader br = new BufferedReader(new FileReader("space.txt"));

			BufferedReader br1 = new BufferedReader(new FileReader("comma.txt"));

			BufferedReader br2 = new BufferedReader(new FileReader("pipe.txt"));

			String str = "";

			

			while((str = br.readLine()) != null)

			{

				li.add(str);

			}

			while((str = br1.readLine()) != null)

			{

				b = str.replaceAll(",", "");		

				li.add(b);

			}

			while((str = br2.readLine()) != null)

			{

				b = str.replaceAll("\\|", "");		

				li.add(b);

			}

			

		}

		catch(Exception e)	

		{

			e.printStackTrace();

		}

	}

	public static void main(String[] args)

	{

		Testcompare r = new Testcompare();

		r.readfiles();	

		for(int i = 0; i < r.li.size(); i ++)

		{

			System.out.println(r.li.get(i));

		}

	}

}

Open in new window

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 24141012
You would do


Collections.sort(li, new Comparator() {

	public int compare(Object o1, Object o2) {

		String[] a1 = ((String)o1).split(" +");

		String[] a2 = ((String)o2).split(" +");

		int result = a1[2].compareTo(a2[2]);

		if (result == 0) {

			result = Integer.parseInt(a1[3]) - Integer.parseInt(a2[3]);

		}

		return result;

	}

});

Open in new window

0
 

Author Comment

by:ravivanjani
ID: 24141871
what does a1[2] and a2[2] contain though??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24141957
The '3rd char in the list'
0
 

Author Comment

by:ravivanjani
ID: 24142009
I did add the code you said to the program but it is giving me exceptions.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24142126
>>
Kournikova Anna F F 6-3-1975 Red
Hingis Martina M F 4-2-1979 Green
Seles Monica H F 12-2-1973 Black
>>

The above is completely different input to that you posted originally
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ravivanjani
ID: 24142228
Ohh yes i apologize...My final input is going to be

Kournikova Anna F F 6-3-1975 Red
Hingis Martina M F 4-2-1979 Green
Seles Monica H F 12-2-1973 Black
Abercrombie Neil Male Tan 2/13/1943
Bishop Timothy Male Yellow 4/23/1967
Kelly Sue Female Pink 7/12/1959
Smith  Steve  D  M  Red  3-3-1985
Bonk  Radek  S  M  Green  6-3-1975
Bouillon  Francis  G  M  Blue  6-3-1975

I want to sort this based on 3 different criteria
gender
date
last name
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24142296
The data are not arranged properly. e.g.

>>Hingis Martina M F 4-2-1979 Green
>>Abercrombie Neil Male Tan 2/13/1943

have dates and colours in opposite ordering

>>Kelly Sue Female Pink 7/12/1959
>>Hingis Martina M F 4-2-1979 Green

have different gender ordering
0
 

Author Comment

by:ravivanjani
ID: 24142374
That is the way it is. Is there a solution to this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24142763
The best way is to ensure that the data are ordered uniformly
0
 

Author Comment

by:ravivanjani
ID: 24142791
ok i have ordered the data...now the data looks like this

Hingis Martina Female 4/2/1979 Green
Kelly Sue Female 7/12/1959 Pink
Kournikova Anna Female 6/3/1975 Red
Seles Monica Female 12/2/1973 Black
Abercrombie Neil Male 2/13/1943 Tan
Bishop Timothy Male 4/23/1967 Yellow
Bonk Radek Male 6/3/1975 Green
Bouillon Francis Male 6/3/1975 Blue
Smith Steve Male 3/3/1985 Red

How do i sort this data now based on the date? I am unable to do that.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 80 total points
ID: 24143022
OK. Try this
Collections.sort(li, new Comparator() {

	DateFormat df = new SimpleDateFormat("d-M-yyyy");

	public int compare(Object o1, Object o2) {

		Date d1 = null;

		Date d2 = null;

		String[] a1 = ((String)o1).split(" +");

		String[] a2 = ((String)o2).split(" +");

		int result = a1[2].compareTo(a2[2]);

		if (result == 0) {

			try {

				result = df.parse(a1[3]).compareTo(df.parse(a2[3]));

			}

			catch(ParseException e) {

				e.printStackTrace();

			}

			if (result == 0) {

				result = a1[0].compareTo(a2[0]);

			}

		}

		return result;

	}

});

Open in new window

0
 

Accepted Solution

by:
ravivanjani earned 0 total points
ID: 24143113
Thank you. It works fine. I am just trying to figure out a few more things.
Thank you again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 64
Starting to learn JAVA, 7 59
servlet  URL Rewriting 1 25
couple of eclipse 5 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now