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. A list of pairs (string, int) + sorting

A list of pairs (string, int) + sorting

Scheduled Pinned Locked Moved C#
questioncsharpalgorithmshelp
4 Posts 4 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.
  • D Offline
    D Offline
    domo_
    wrote on last edited by
    #1

    Hi there, I'm kind of new to C# (that might explain this question). I stuck with my code when I have to: 1. implement a list of pairs (string, int) 2. sort it by int Could you give me some hints on points 1 & 2? Thanks Domo_

    A N L 3 Replies Last reply
    0
    • D domo_

      Hi there, I'm kind of new to C# (that might explain this question). I stuck with my code when I have to: 1. implement a list of pairs (string, int) 2. sort it by int Could you give me some hints on points 1 & 2? Thanks Domo_

      A Offline
      A Offline
      afinnell
      wrote on last edited by
      #2

      The first step is to define a structure to hold your data. I used the property accessors in this case because it is part of my habit. These aren't required but are there for good measure. The second step is to define a class that will determine how you will sort your data. This class derives from the IComparer interface and implements the Compare method. For more robust code you could check the instance type of the objects to be sure they are of the Pair type. The third step is to sort your array of data. This can be done by using the static Sort utility method on the Array class. This method will accept any standard Array and sort it using your comparer.

      using System;
      using System.Collections;

      namespace ConsoleApplication1
      {

      /// /// Contains two entities. This would be more flexable
      /// if we were using templates.
      /// 
      class Pair
      {	
      	public String MyString
      	{
      		get
      		{
      			return \_string;
      		}
      		set
      		{
      			\_string = value;
      		}
      	}
      
      	public int MyInt
      	{
      		get
      		{
      			return \_int;
      		}
      		set
      		{
      			\_int = value;
      		}
      	}
      
      	private int \_int;
      	private String \_string;
      }
      
      /// /// This class will do the comparing. Change the order of
      /// left and right to obtain an descending order.
      /// 
      class PairComparer : IComparer
      {
      	#region IComparer Members
      
      	public int Compare(object x, object y)
      	{
      		Pair left = (Pair)x;
      		Pair right = (Pair)y;
      		return left.MyInt.CompareTo(right.MyInt);
      	}
      
      	#endregion
      }
      
      /// /// Summary description for Class1.
      /// 
      class Class1
      {
      	/// /// The main entry point for the application.
      	/// 
      	\[STAThread\]
      	static void Main(string\[\] args)
      	{
      		Pair\[\] pairs = new Pair\[3\];
      		pairs\[0\] = new Pair();
      		pairs\[0\].MyInt = 10;
      		pairs\[0\].MyString = "Ten";
      
      		pairs\[1\] = new Pair();
      		pairs\[1\].MyInt = 3;
      		pairs\[0\].MyString = "Three";
      
      		pairs\[2\] = new Pair();
      		pairs\[2\].MyInt = 1;
      		pairs\[2\].MyString = "One";
      
      		// Sort the array the way we want to 
      		// by specifying the comparer
      		Array.Sort(pairs, new PairComparer());
      
      		// Print out the results to see if the comparing
      		// was successful
      		foreach (Pair pair in pairs)
      		{
      			Console.WriteLine(
      				"Pair: {0} {1}" , 
      				pair.MyInt, 
      				pair.MyString);
      		}
      
      	}
      }
      

      }

      - Drew

      1 Reply Last reply
      0
      • D domo_

        Hi there, I'm kind of new to C# (that might explain this question). I stuck with my code when I have to: 1. implement a list of pairs (string, int) 2. sort it by int Could you give me some hints on points 1 & 2? Thanks Domo_

        N Offline
        N Offline
        Nicholas Cardi
        wrote on last edited by
        #3

        //Wrap this in a command button click event System.Collections.ArrayList arylist = new ArrayList(); for (int i = 10; i >= 0; i--) { pairs values; values.stringvalue = i.ToString(); values.intvalue = i; arylist.Add(values); } IComparer comp = new MyCompare(); arylist.Sort(comp); for(int i = 0; i<=10; i++) { pairs values = (pairs)arylist[i]; MessageBox.Show(values.intvalue.ToString() + " " + values.stringvalue); } //end wrap private class MyCompare: System.Collections.IComparer { public int Compare(object x, object y) { pairs valuex = (pairs)x; pairs valuey = (pairs)y; if (valuex.intvalue < valuey.intvalue) return -1; if (valuex.intvalue > valuey.intvalue) return 1; return 0; } } private struct pairs { public string stringvalue; public int intvalue; } Forever Developing

        1 Reply Last reply
        0
        • D domo_

          Hi there, I'm kind of new to C# (that might explain this question). I stuck with my code when I have to: 1. implement a list of pairs (string, int) 2. sort it by int Could you give me some hints on points 1 & 2? Thanks Domo_

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          If you know the upperbound of the int and its not too high and the values are not too sparse, just create a string array. :p xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          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