# Sorting .xml data

**URL:** <https://forum.kirupa.com/t/sorting-xml-data/209025>\
**Category:** Uncategorized\
**Created:** [December 5, 2006, 10:21am UTC](https://forum.kirupa.com/t/sorting-xml-data/209025 "2006-12-05T10:21:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dunkyb123](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@dunkyb123](https://forum.kirupa.com/u/dunkyb123)\
**Post date:** [December 5, 2006, 10:21am UTC](https://forum.kirupa.com/t/sorting-xml-data/209025/1 "2006-12-05T10:21:47Z")

</div>

Hello all

Iam trying to get some .xml data to sort in my website but i am havinga problem, I followed the tutorial : [http://www.kirupa.com/web/xml/examples/sortedgradeslist.htm](http://www.kirupa.com/web/xml/examples/sortedgradeslist.htm) which worked fine for the data.

I edited the code and have made my own version, but i cannot figure out how to get the sorting right, can somone point me in the right direction.

```auto
PopulateLists = function(xml_array){
    name_txt.text = teamscore_txt.text = kills_txt.text = deaths_txt.text = score_tx.text = "";
    for (var i=0; i<xml_array.length; i++){
        var student = xml_array*.attributes;
        name_txt.text += student.first + "
";
        teamscore_txt.text += student.teamscore + "
";
        kills_txt.text += student.kills + "
";
        death_txt.text += student.deaths + "
";
        score_txt.text += student.score + "
";
        }
}

var students_array;
var grades_xml = new XML();
grades_xml.ignoreWhite = true;
grades_xml.onLoad = function(success){
    if (success){
        var grades = this.firstChild;
        students_array = grades.childNodes;
        PopulateLists(students_array);
    }else trace("Error loading XML file.");
}

grades_xml.load("olan31307.xml");

FIRST_SORT = function(a,b){
    return a.attributes.first > b.attributes.first;
}
TSCORE_SORT = function(a,b){
    // since attributes are defined as strings, parseFloat is used
    // to convert them to numeric values for the GPA compare function
    return parseFloat(a.attributes.tscore) < parseFloat(b.attributes.tscore);
}
KILL_SORT = function(a,b){
    // since attributes are defined as strings, parseFloat is used
    // to convert them to numeric values for the GPA compare function
    return parseFloat(a.attributes.kills) < parseFloat(b.attributes.kills);
}
OSCORE_SORT = function(a,b){
    // since attributes are defined as strings, parseFloat is used
    // to convert them to numeric values for the GPA compare function
    return parseFloat(a.attributes.oscore) < parseFloat(b.attributes.oscore);
}
DEATH_SORT = function(a,b){
    // since attributes are defined as strings, parseFloat is used
    // to convert them to numeric values for the GPA compare function
    return parseFloat(a.attributes.deaths) < parseFloat(b.attributes.deaths);
}

var current_sort;
SortListBy = function(sortType){
    if (current_sort == sortType){
        students_array.reverse();
    }else{
        // sort does not effect XML
        students_array.sort(sortType);
    }
    current_sort = sortType;
    PopulateLists(students_array);
}

name_btn.onRelease = function(){
    SortListBy(FIRST_SORT);
}
tscore_btn.onRelease = function(){
    SortListBy(TSCORE_SORT);
}
kill_btn.onRelease = function(){
    SortListBy(KILL_SORT);
}
death_btn.onRelease = function(){
    SortListBy(DEATH_SORT);
}
oscore_btn.onRelease = function(){
    SortListBy(OSCORE_SORT);
}

restore_btn.onRelease = function(){
    var grades = grades_xml.firstChild;
    students_array = grades.childNodes;
    PopulateLists(students_array);
}

```

It sorts out the mane corectly A - Z but i cant get the rest to sort properly

My file can be viewed here: [http://www.theoaps.co.uk/Support%20Files/OLAN31307swf.swf](http://www.theoaps.co.uk/Support%20Files/OLAN31307swf.swf)

As you will see the last 2 colums do not sort but they duplicate the data :upset:
