Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends, I have one task, sort the number, that are in alphabetic format For example:

VB
(one,three,four,seven,eight)



this content sorted like this

VB
(eight, four, three, seven, one)


any possible way available for sort this in correct format like below

VB
one, three, four, seven, eight
Posted
Updated 9-Aug-15 23:14pm
v2
Comments
Arasappan 10-Aug-15 5:21am    
Is this example or u can only these alphabets to sort.....
Member 11724596 10-Aug-15 5:48am    
this is for example
F-ES Sitecore 10-Aug-15 5:29am    
Google how to convert words into numbers, use that code to convert your "one", "two" etc into 1, 2 etc. Then order that list. Google for how to order a list of numbers if you don't know how to do that.

There is no built in functionality to short numbers by their names, but only by values...The result you got is of alphabetic sorting of course...
The only way is to write your own sort method that knows how to handle this case...
A possible ease may be using List<T>'s Sort(IComparer) method to handle the issue...
https://msdn.microsoft.com/en-us/library/234b841s(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Maciej Los 10-Aug-15 5:41am    
5ed!
When you have an array of strings, the comparison that sorting would use would be based on a character-by-character examination of the string content - so you need to convert the string values to their equivalent numeric values before you can order them. Unfortunately, there is no built in way to do that.

One solution would be to create a collection of the number names and use Linq:
C#
            private List<string> names = new List<string>() { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
...
            string[] unsorted = new string[] { "eight", "four", "three", "seven", "one" };
            string[] sorted = unsorted.OrderBy(s => names.IndexOf(s)).ToArray();
To get the values in descending order, you'd either reverse the order in the initial array, or use the Linq Reverse method.

But...This would be a lot of work to use for a significant range of numbers!

[edit]Typos[/edit]
 
Share this answer
 
v2
In addition to solution 1 by Kornfeld Eliyahu Peter[^] i do recommend to use Dictionary[^] object tohgether with Linq.

C#
//create dictionary object
//Key - integer value, Value - string representatioin of number
Dictionary<int, string> dic = new Dictionary<int, string>();
//add values
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "three");
dic.Add(4, "four");
dic.Add(5, "five");
dic.Add(6, "six");
dic.Add(7, "seven");
dic.Add(8, "eight");
dic.Add(9, "nein");
dic.Add(10, "ten");

//your list
List<string> mynumbers = new List<string>(){"eight", "four", "three", "seven", "one"};

//Linq query to sort data by numeric values
var resultlist = from mn in mynumbers
    join d in dic on mn equals d.Value
    orderby d.Key
    select mn;

//result list is IEnumerable collection of string:
//one 
//three 
//four 
//seven 
//eight 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900