We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › sort values by size // XML
Page Index Toggle Pages: 1
sort values by size // XML (Read 538 times)
sort values by size // XML
Jan 22nd, 2009, 9:20pm
 
I have a processing script which parses me the following data in an xml - file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<datatable>
 <person>
   <height value_height="190"/>
   <width value_width="90"/>
 </person>
 <person>
   <height value_height="130"/>
   <width value_width="80"/>
 </person>
 <person>
   <height value_height="200"/>
   <width value_width="20"/>
 </person>
</datatable>

I now want to work with these values and sort them by size. So both values value_height and value_width have to be put in order.
Can you give me some hints how to sort those values? I work with the proxml library.
thanks in advance!
Re: sort values by size // XML
Reply #1 - Jan 25th, 2009, 6:28pm
 
no idea how to solve this problem Smiley ?
Re: sort values by size // XML
Reply #2 - Jan 25th, 2009, 7:03pm
 
I guess I missed this post.

Do you have any code that actually parses this file?

Let's start with modeling your problem.  You will want to create a Person class.  Here's a framework:

Code:

ArrayList personList; //...or people, if you like.
//setup(){
 personList = new ArrayList();
 //parse xml
 for(...){//for each person
   personList.add(new Person(width, height);
 }
//}




class Person {
 float height;
 float width;
 Person(float height, float width){
    this.height = height;
    this.width = width;
 }
}




the rest is pretty easy.  You make a comparator class for your "size" comparison, then use the comparator on sort(collections, comparator)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html

Give it a pass. If you get stuck, just come back here with your code Smiley
Re: sort values by size // XML
Reply #3 - Jan 28th, 2009, 1:07am
 
this is the code i use to parse the xml file written above in my last post. I used the piece of code you gave me as well....

----------------------------------------------
Code:

import proxml.*;
ArrayList personList;
float height_p;
float width_p;
StringBuffer buffer_height;
StringBuffer buffer_width;
String string_height;
String string_width;
int int_height;
int int_width;
int count;
PImage back;
XMLElement Data;
XMLInOut xmlInOut;
int posX = 10;
int posY = 10;
void setup() {
 size (800,450);
 noStroke();
 smooth();
 personList = new ArrayList();
 buffer_height = new StringBuffer();
 buffer_width = new StringBuffer();
 xmlInOut = new XMLInOut(this);
 try{
   xmlInOut.loadElement("datatable1.xml");
 }
 catch(Exception e){
   //if the xml file could not be loaded it has to be created
   xmlEvent(new XMLElement("datatable"));
 }
}
void draw(){
}
void keyReleased () {
 if (keyCode == 10) {
   count ++;
 }
 if (keyCode == 8 ) {
   if (buffer_height.length() > 0) {
     buffer_height.deleteCharAt (buffer_height.length()-1);
   }
 }
 if (count == 0 && key != ENTER) {
   buffer_height.append(key);    
   string_height = buffer_height.toString();
   int_height = int(string_height);
 }
 if (count == 1 && key != ENTER) {
   buffer_width.append(key);    
   string_width = buffer_width.toString();
   int_width = int(string_width);
 }
 if (count == 2) {
   analyze();
 }
}
void analyze () {
 rect (posX,posY, int_width, int_height);
 count = 0;
 posX += int_width+10;
 save();
 setup();
}
void xmlEvent(XMLElement element){
 Data = element;
 initData();
 //initialise PImage for background
 back = new PImage(width,height);
 loadPixels();
 back.pixels = pixels;
}
class Person {
 float height_p;
 float width_p;
 Person (float height_p, float width_p) {
   this.height_p = height;
   this.width_p = width;
 }
}
void initData(){
 XMLElement value;
 XMLElement height;
 XMLElement width;
 for(int i = 0; i < Data.countChildren();i++){
   value = Data.getChild(0);
   height = value.getChild(0);
   width = value.getChild(1);
   personList.add(new Person (width_p,height_p));
 }
}
void save(){
 XMLElement value = new XMLElement("person");
 Data.addChild(value);
 XMLElement height = new XMLElement("height");
 height.addAttribute("value_height", string_height);
 value.addChild(height);
 XMLElement width = new XMLElement("width");
 width.addAttribute("value_width", string_width);
 value.addChild(width);
 xmlInOut.saveElement(Data,"datatable1.xml");
}

----------------------------------------------
so i now have to read the data out of the xml file into an ArrayList?
As you posted?
Hmm..and how can I use the comparator class now?

Thanks for helping me, I hope my code is not too confusing...

Re: sort values by size // XML
Reply #4 - Jan 28th, 2009, 1:42am
 
maybe this will help:

Code:

Person a,b,c,d;
ArrayList people;
void setup(){

a = new Person(30.0 , 40.0);
b = new Person(23.0 , 10.0);
c = new Person(22.0 , 222.0);
d = new Person(1.0 , 21.0);

people = new ArrayList();

people.add(a);
people.add(b);
people.add(c);
people.add(d);


noLoop();
}


void draw(){
for(int i=0; i< people.size(); i++){
Person person = (Person)people.get(i);
person.report();
}

Collections.sort(people);

println("bam");
for(int i=0; i< people.size(); i++){
Person person = (Person)people.get(i);
person.report();
}




}


class Person implements Comparable{
float height_p;
float width_p;
Person (float height_p, float width_p) {
this.height_p = height_p;
this.width_p = width_p;
}

void report(){
println("width:" + width_p + " height:"+ height_p);
}

public int compareTo(Object o){
Person p = (Person)o;
return (int)(height_p - p.height_p) + (int)(width_p - p.width_p);
}
}





Re: sort values by size // XML
Reply #5 - Jan 31st, 2009, 12:17am
 
hey,
thanks so far. i worked through your code and it helped me...but, i tried to modify it.
in my case i won't have Person a, b, c, d... but many more.
So you type in a number and then it has to be implemented into the ArrayList.
I tried it this way, but it does not work properly yet. It's a combination of my first code and yours.
So all I want is to type in a number, press Enter and then the number is in the ArrayList. And you can go on for as many numbers as you want. I think it's just a little error, but i don't get it... Undecided
thanks again Wink


Code:
Person a;
StringBuffer buffer_height;
String string_height;
ArrayList personList;
int count;
int int_height;

void setup(){
buffer_height = new StringBuffer();
personList = new ArrayList();
}

void keyReleased () {
if (keyCode == 10) {
count ++;
}
if (count == 0 && key != ENTER) {
buffer_height.append(key);
string_height = buffer_height.toString();
int_height = int(string_height);
}
if (count == 1 && key != ENTER) {
analyze();
}
}

void draw(){
println (int_height);
println (a);
}

void analyze() {
// I tried this to put the current value you've just typed into the ArrayList
a = new Person (int_height);
personList.add(a);
}

class Person implements Comparable{
int int_height;
Person (int int_height) {
this.int_height = int_height;
}
void report(){
println("height:"+ int_height);
}
public int compareTo(Object o){
Person p = (Person)o;
return (int)(int_height - p.int_height);
}
}
Re: sort values by size // XML
Reply #6 - Feb 3rd, 2009, 1:47am
 
Quote:
people = new ArrayList();
a = new Person(30.0 , 40.0);
b = new Person(23.0 , 10.0);
c = new Person(22.0 , 222.0);
d = new Person(1.0 , 21.0);
 
people.add(a);
people.add(b);
people.add(c);
people.add(d);
noLoop();
}  
void draw(){
 for(int i=0; i< people.size(); i++){
   Person person = (Person)people.get(i);
   person.report();
 }
 Collections.sort(people);
 println("bam");
 for(int i=0; i< people.size(); i++){
   Person person = (Person)people.get(i);
   person.report();
 }  
}


When using println (people) I get this as a result...
[Temporary_5781_7270$Person@789144, Temporary_5781_7270$Person@893efe, Temporary_5781_7270$Person@86c6b2, Temporary_5781_7270$Person@5ee671]

I thought the numbers assigned above should be printed...I can't work out what these results should mean????
any idea? thanks again!!!
Re: sort values by size // XML
Reply #7 - Feb 3rd, 2009, 1:08pm
 
those numbers are pointers into memory for the classes you are printing - completely useless.

if you implement a public String toString() method in your person class it'll use that when printing Persons.

or it might not because it doesn't know that people is an ArrayList full of Persons. you might need a cast.
Page Index Toggle Pages: 1