Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Custom sorting algorithm
Message
De
04/05/2008 08:25:35
 
 
À
04/05/2008 02:34:32
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01315003
Message ID:
01315007
Vues:
18
>Hi,
>I have item as below:
>
>
>ItemName          BeforeItem    AfterItem
>------------------------------------------
>"First"
>"Second"
>"Third"
>"B4Second"        "Second"
>"BetweenSecond"   "Second"      "B4Second"
>
>
>
>I would like to implement IComparable to sort them so that it could be as below:
>
>First
>B4Second
>BetweenSecond
>Second
>Third
>
>Please advice
>
>Thank you
___________
There is nothing in your definition that states that
- First has to come before Second
- Second has to come before Third


Anyway, in order to accomplish such things you can do a topological sort, which will give you likely the following output
since there is no dependency between any of First, Second and Third
Third
First
B4Second
BetweenSecond
Second
If we suppose that rows with empty BeforeItem and empty AfterItem imply a natural order, then I can produce your output


sample program
		static void doit()
		{
			string [,] s  = 
				// 
			{	{ "First", null, null}, 
				{ "Second", null, null }, 
				{ "Third", null, null }, 
				{ "B4Second", "Second", null },
				{ "BetweenSecond", "Second", "B4Second" }
			} ;

			SortThem(s, true);
		}

		static void SortThem( string[,] s, bool respectInputOrder) 
		{

			var v = new Graph.TopologicalSort<string>();
			int i;
			string successor, predecessor, lastItem = null;


			for (i = 0; i < s.GetLength(0); i++)
			{

				// 0 and 1
				predecessor = s[i, 0];
				successor = s[i, 1];

				if (respectInputOrder && (successor == null) && (s[i, 2] == null) )
				{
					if (lastItem != null && !v.Edge(predecessor, lastItem))
							throw new Exception("program error");

					lastItem = predecessor ;
				}

				if (successor == null)
				{
					if (!v.Edge(predecessor))
						throw new Exception("program error");
				}
				else
				{	
					if( !v.Edge(successor, predecessor) )
						throw new Exception("program error");
				}
				
				// 1 && 2
				
				successor = predecessor ; // s[i, 0]
				predecessor = s[i, 2];

				if (predecessor != null)
				{
					if (!v.Edge(successor, predecessor))
						throw new Exception("program error");
				}
	
			}

			// Sort them
			Queue<string> output;

			if (!v.Sort(out output))
			{	// cycles
				Console.WriteLine("a cycle has been detected");
				while (output.Count > 0)
					Console.WriteLine(output.Dequeue());

			}
			else
			{
				Console.WriteLine("Evaluation order is");
				while (output.Count > 0)
					Console.WriteLine(output.Dequeue());
			}
		}
Topological sort can be downloaded here: http://www.atoutfox.com/articles.asp?ACTION=FCONSULTER&ID=0000000526

UPDATE
Changed download to work with IEquatable instead of IComparable
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform