Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<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

XML
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

XML
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>Business Department </dept-data></value>
		</data>
	</student>
	<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>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>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>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>58123</value>
		</data>
		<data>
			<key>dept</key>
			<value><?xml version="1.0" encoding="UTF-8"?>Description> Physics Department </dept-data></value>
		</data>
	</student>
</students>


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

Thanks
Posted

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900