2 Replies - 1235 Views - Last Post: 17 December 2012 - 06:02 AM Rate Topic: -----

#1 alec100   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-December 12

Sorting Array in Same Order as Another Array

Posted 15 December 2012 - 04:01 PM

Hi I'm new to this forum, and don't usually ask for programming help, but instead tend to figure out things for myself. But This problem has bugging me for the past several days and I am really unsure how to continue.

I am writing a game in C#/XNA that utilizes a high score score file in XML format. I Have got it so the games writes to the file when you enter your name. But Am having great difficulty getting it to read back the high scores in the correct order (starting with top and going to bottom.)

My XML file looks like this

<?xml version="1.0" encoding="utf-8"?>
  <player name="Alec">
  <player name="Fred">
  <player name="Bob">
  <player name="James">
  <player name="Phil">

and I have the following code

            var highScores = HighScoreFile.GetElementsByTagName("score");

            topScores = new string[3,highScores.Count];

            int[] listOfScores = new int[highScores.Count];
            string[] listOfPlayers = new string[highScores.Count];
            string[] listOfLevels = new string[highScores.Count];

            for (int i = 0; i < highScores.Count; i++)
                listOfPlayers[i] = highScores.Item(i).ParentNode.Attributes["name"].Value.ToString();
                listOfScores[i] = int.Parse(highScores.Item(i).InnerText.ToString());
                listOfLevels[i] = highScores.Item(i).NextSibling.InnerText.ToString();


            for (int i = 0; i < highScores.Count; i++)
                topScores[0,i] = listOfPlayers[i];
                topScores[1,i] = listOfScores[i].ToString();
                topScores[2,i] = listOfLevels[i];

I know that this code doesn't do what I want it to because it only puts The Score in Order using the Array.sort Method. This means that only the score displays correctly and the Players Name and Level reached Display of the first (or whatever number is specified) <player> node in the XML file instead of the correct one (the One Assigned to the Highest, Then Second Highest for The Next One etc.). I Understand What The Code I posted Does. And really want to sort the other two string arrays into the same order as the listOfScores Integer Array, so That the Correct name and level are shown when it is displayed. But I am unsure how to do this. I have also tried sorting the XML file so it is in the correct order in the first place, but that proved to be a real headache, and seems like a slightly stupid concept anyway due to an XML's structure. Thanks in Advanced


This post has been edited by tlhIn`toq: 15 December 2012 - 06:55 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Array in Same Order as Another Array

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Sorting Array in Same Order as Another Array

Posted 15 December 2012 - 07:02 PM

I doubt this code compiles and runs. You seem to be mixing up VB.NET and C# when it comes to element access.


listOfPlayers[i] = highScores.Item(i).ParentNode.Attributes["name"].Value.ToString();

.Item(i) only works if you have a METHOD named Item that takes an int as a parameter. It looks more like the VB.NET way to indicate an element from an array which in C# is done with [] not ()

It would be a lot better if you weren't trying to micromanage the reading and writing process.

Create a "Score" class with name, and level and points etc.

Then make a List<Score> that represents all of your scores.

Then serialize/deserialize the list.

Separating data from GUI - PLUS - serializing the data to XML

You also need to get away from these primitive arrays and use better collections like List

Stop trying to keep lots of parallel arrays like this: An array of Playsers, an array of Score and an Array of level. That's just a nightmare. You need to be thinking in a better Object Oriented way. A class of "score" with properties for name, points, level etc.

Have a read of the classes tutorial linked in my signature block.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon

Reputation: 7361
  • View blog
  • Posts: 15,285
  • Joined: 16-October 07

Re: Sorting Array in Same Order as Another Array

Posted 17 December 2012 - 06:02 AM

Parallel arrays, almost always a bad idea.

Use a class:
class Player {
	string Name { get; set; }
	int Score { get; set; }
	int Level { get; set; }

Use a List.

Load that:
List<Player> items = new List<Player>();
foreach(var ele in HighScoreFile.GetElementsByTagName("player")) {
	items.add( new Player() {

Use a List sort, comparing the score values.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1