Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. newbie: sort values in a text.txt and display by name?

newbie: sort values in a text.txt and display by name?

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hristo1977
    wrote on last edited by
    #1

    Hi! I have a textfile which contains Name and Value (each name has a value). I want to display ONLY the names in a listbox. And it should be the names with the 10 highest scores. The highest score first... I do know how to fill the listbox with all the names. But I cant get around to the other...any suggestions?

    Newbie untill I die! :-)

    L 1 Reply Last reply
    0
    • H hristo1977

      Hi! I have a textfile which contains Name and Value (each name has a value). I want to display ONLY the names in a listbox. And it should be the names with the 10 highest scores. The highest score first... I do know how to fill the listbox with all the names. But I cant get around to the other...any suggestions?

      Newbie untill I die! :-)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Show us how your txt is formatted If it's something like "name=value" then you could do something like that a) string[] lines = File.ReadAllLines("highscores.txt") b) create a class:

      struct Highscore
      {
      public string name;
      public int score;
      }

      b)

      List<Highscore> scores = new List<Highscore>();

      foreach line in lines {
      Highscore h = new Highscore();
      h.name = line.Substring(0, line.IndexOf('='));
      h.score = int.Parse(line.Substring(line.IndexOf('=')+1));
      scores.Add(h);
      }

      c)

      scores.Sort(... some IComparer to sort for score ...)

      d)

      for(int i=0; i<10; i++)
      listbox.Add(scores[i].name);

      regards

      H 1 Reply Last reply
      0
      • L Lost User

        Show us how your txt is formatted If it's something like "name=value" then you could do something like that a) string[] lines = File.ReadAllLines("highscores.txt") b) create a class:

        struct Highscore
        {
        public string name;
        public int score;
        }

        b)

        List<Highscore> scores = new List<Highscore>();

        foreach line in lines {
        Highscore h = new Highscore();
        h.name = line.Substring(0, line.IndexOf('='));
        h.score = int.Parse(line.Substring(line.IndexOf('=')+1));
        scores.Add(h);
        }

        c)

        scores.Sort(... some IComparer to sort for score ...)

        d)

        for(int i=0; i<10; i++)
        listbox.Add(scores[i].name);

        regards

        H Offline
        H Offline
        hristo1977
        wrote on last edited by
        #3

        peter 0,333333333333333 peter 1,33333333333333 thats how its look like

        Newbie untill I die! :-)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups