Click here to Skip to main content
12,444,383 members (49,852 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
<students>
	<student>
		<data>
			<key>stu-id</key>
			<value>56170</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>Computer Science Department </dept-data></value>
		</data>
	</student>
	<student>
		<data>
			<key>stu-id</key>
			<value>56171</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>Business Department </dept-data></value>
		</data>
	</student>
	<student>
		<data>
			<key>stu-id</key>
			<value>50278</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>Economics Department </dept-data></value>
		</data>
	</student>
	<student>
		<data>
			<key>stu-id</key>
			<value>60241</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>English Department </dept-data></value>
		</data>
	</student>
	<student>
		<data>
			<key>stu-id</key>
			<value>58123</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description> Physics Department </dept-data></value>
		</data>
	</student>
	<student>
		<data>
			<key>stu-id</key>
			<value>56126</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>Chemistry Department </dept-data></value>
		</data>
	</student>
</students>

need to sort the above file with java so I wrote a comparator class

public class Comp implements Comparator<StudentType> {
 

 

        public class Comp implements Comparator<StudentType> {
 

 

        public int compare(StudentType o1,StudentType o2)
            return (o1.getStudentList().get(1).getValue().compareTo(o2.getStudentList().get(1).getValue()));
 

        }
 
}

Calling as

List<StudentType> students = response.getStudents().getStudentList();
 

          Collections.sort(students, comparator);


New sorted file looks like this

<students>
<student>
		<data>
			<key>stu-id</key>
			<value>56171</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description>

I don't know why the camparator class is sorting and deleting some data and duplicating it

Thanks
Posted 6-Feb-13 5:48am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html[^]

How do you store the values from the XML?
some Collections have a tendency to overwrite values that are already in.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100