The online racing simulator
Sorting.
(24 posts, started )
Sorting.
Hello. I am making application that will fix score and then I want it to make list of players like 1st place 2nd etc. Some example:
foreach (clsConnection c in Connections)
{
if (c.Username != "")
{




InSim.Send_BTN_CreateButton(c.PlayerName, Flags.ButtonStyles.ISB_LEFT, 5, 15, LocationX, 1, ButtonID1, C.UniqueID, 40, false);
InSim.Send_BTN_CreateButton("^7 " + c.Score, Flags.ButtonStyles.ISB_CLICK, 5, 10, LocationX, 16, ButtonID2, (C.UniqueID), 40, false);

So how to make it to do that?
You'd need to write out your users into a temporary array (as I assume you're not using a database like MySQL), then use a C# method like Array.Sort, and then loop over that new, sorted temporary array to draw the buttons.

If you are using a database, you could just sort by points/cash when retrieving it (using a DB query) and then spit it out in the order you receive it from the DB.
Those points are saved in each user file. Could you please show some example with code?
Not without seeing the file format first.
More seriously, you want to loop through either your connection list (if you only care about users that are in the server) or your entire user list and create 2 arrays, one containing only usernames and another containing the desired sortable items (points,cash, crashes).

After you have those two arrays, use C#'s array.sort to do the sort, then output the results however you want. If you need pagination, then you need to figure out how you want to slice the sorted array, store the current set for each connection and output.
Quote from dawesdust_12 :More seriously, you want to loop through either your connection list (if you only care about users that are in the server) or your entire user list and create 2 arrays, one containing only usernames and another containing the desired sortable items (points,cash, crashes).

A map would be the structure to use in this case. In C# terminology it would be a Dictionary<string, List<int>>


<?php 
void SortAndPrint
()
  {
    
Dictionary<string, List<int>> scores = new Dictionary<string, List<int>>();

    
string[] files Directory.GetFiles(@"./""*.txt");
    if (
files.Length == 0) {
      
Console.WriteLine("No files.");
      return;
    }

    foreach (
string f in files)
    {
      
string name f.Substring(2f.Length 6); /* No trailing ".txt" and leading "./" */

      
string[] lines;
      try
      {
        
lines System.IO.File.ReadAllLines(f);
      }
      catch (
Exception e)
      {
        
Console.WriteLine(e.ToString());
        continue;
      }

      
scores.Add(name, new List<int>());
      foreach (
string s in lines)
      {
        
int val;
        try
        {
          
val Convert.ToInt32(s);
        }
        catch (
Exception)
        {
          continue;
        }
          
scores[name].Add(val);
      }
      
scores[name].Sort();
    }

    foreach (
KeyValuePair<string, List<int>> pair in scores)
    {
      
Console.WriteLine("--- " pair.Key);
      foreach (
int i in pair.Value)
      {
       
Console.WriteLine(i);
      }
    }
  }
?>

That's fair. I don't know C#, I'm used to PHP with its infinitely useful associative arrays, or JS where you use objects as nice associative array-like items.

Nevermind me avoiding giving code samples, instead preferring people to figure it out with guidance based on general concepts so they can actually learn to program.
Thanks MadCatX awesome as always So I save those score like this - everyone have his Username.txt file and in that file he has his score. Now it prints me out users with their score in their username.txt order as they are in Users folder. How to get it to print with biggest score first?

EDIT: I got that it sorts each score in each user file. But I need it to sort all scores from all user files and print out Users from biggest to lowest score.
Give a man a fish he eats for a day, teach a man to fish he eats for a life time.
Quote from Dygear :Give a man a fish he eats for a day, teach a man to fish he eats for a life time.

Sell a man a fish, and you're rich.
Quote from Dygear :Give a man a fish he eats for a day, teach a man to fish he eats for a life time.

what if he lives in the middle of the sahara?
Help please, this is just so close!
Eat fish be happy:laola:
Quote from Shotglass :what if he lives in the middle of the sahara?

The term, "shit out of luck", comes to mind.
Why not just use the new-keyword to create more fish?

public class Trout extends Fish { ...

So I need to add something like this?

<?php 
  void SortAndPrint
()
        {
            
Dictionary<string, List<int>> scores = new Dictionary<string, List<int>>();
            
Dictionary<string, List<int>> results = new Dictionary<string, List<int>>();

            
string[] files Directory.GetFiles(@"users""*.txt");
            if (
files.Length == 0)
            {
                
MsgAll("No files.");
                return;
            }

            foreach (
string f in files)
            {
                
string name f.Substring(6f.Length 10); /* No trailing ".txt" and leading "./" */

                
string[] lines;
                try
                {
                    
lines System.IO.File.ReadAllLines(f);
                }
                catch (
Exception e)
                {
                    
MsgAll(e.ToString());
                    continue;
                }

                
scores.Add(name, new List<int>());
                foreach (
string s in lines)
                {
                    
int val;
                    try
                    {
                        
val Convert.ToInt32(s);
                    }
                    catch (
Exception)
                    {
                        continue;
                    }
                    
scores[name].Add(val);
                }
                
scores[name].Sort();

                
results.Add(name, new List<int>());
                foreach (
int s in scores[name])
                {
                    
                    
results[name].Add(Convert.ToInt32(scores[name]));
                }
                
results[name].Sort();


            }

            foreach (
KeyValuePair<string, List<int>> pair in results)
            {
                
MsgAll("--- " pair.Key);
                foreach (
int i in pair.Value)
                {
                    
MsgAll("");
                }
            }
        }
?>

Cant get it to work
This is what a solution used by an actual programmer could look like. Please note that this serves absolutely no educational purpose unless you're already familiar with slightly more advanced OOP and C#.


<?php 
using System
;
using System.Collections.Generic;
using System.IO;

public class 
PlayersScore IComparable
{
  public 
string Name getset; }
  public 
Int32 Score getset; }

  public 
PlayersScore(string NameInt32 Score)
  {
    
this.Name Name;
    
this.Score Score;
  }

  public 
int CompareTo(Object other)
  {
    
PlayersScore otherS null;
    try
    {
      
otherS = (PlayersScore)other;
    }
    catch (
InvalidCastException ex)
    {
      throw 
ex;
    }

    if (
this.Score otherS.Score)
      return -
1;
    else if (
this.Score otherS.Score)
      return 
1;
    else
      return 
String.Compare(this.NameotherS.NameSystem.StringComparison.OrdinalIgnoreCase);
  }
}

public class 
SortScores
{
  static 
int Main()
  {
    List<
PlayersScorescores = new List<PlayersScore>();

    
string[] files Directory.GetFiles(@"./""*.txt");
    if (
files.Length 1)
    {
      
Console.WriteLine("No files.");
      return 
1;
    }

    foreach(
string f in files)
    {
      
string name f.Substring(2f.Length 6);

      
string[] lines;
      try
      {
        
lines System.IO.File.ReadAllLines(f);
      }
      catch (
Exception ex)
      {
        
Console.WriteLine("File \"" f.Substring(2) + "\" cannot be read.");
        
Console.WriteLine(ex.ToString());
        continue;
      }

      foreach(
string line in lines)
      {
        
Int32 score
        try
        {
           
score Convert.ToInt32(line);
        }
        catch (
Exception ex)
        {
          
Console.WriteLine("Line \"" line "\" cannot be converted to Int32.");
          
Console.WriteLine(ex.ToString());
          continue;
        }

        
scores.Add(new PlayersScore(namescore));
      }
    }

    
scores.Sort();

    foreach(
PlayersScore ps in scores)
    {
      
Console.WriteLine(ps.Score " (" ps.Name ")");
    }

    return 
0;
  }
}
?>

Thanksm but how can I make it to send buttons to LFS. I cant get it to print results in any way to LFS.
I don't think you need all these complicated things above...

Can you get the 1st , 2nd and 3rd Username ?
No I cant.
He stores scores for each user in a separate file, therefore it's necessary to:
- Create a "username:score" pair, which means reading all files
- Store each pair into an array
- Sort the objects in the array by score

There are of course multiple ways how to accomplish this, but this one seems to me as the most straightforward and flexible and it also uses the existing .NET tools for sorting.

One of the alternatives could be something like

<?php 
List<stringnames;
List<
Int32scores;
foreach(
file)
{
  foreach(
line)
  {
    
names.Add(name);
    
scores.Add(Convert.Int32(line));
  }
}
Array.
Sort(scoresnames);
?>

but that's far less elegant.
Mybe I need to make different storage? If yes could you tell me how would it look like?

Sorting.
(24 posts, started )
FGED GREDG RDFGDR GSFDG