Difficult to sort
-
Here, I solved this with C# and Linqpad. Here are the steps: 1. Get LINQPad at http://www.linqpad.net/[^] It's a great free tool which allows you to run C# as a script 2. Copy the code below to LINQPad 3. Change the <yourPathToFiles> to the path to your files. 4. run Notice that it uses an enumeration to set the value automatically of each string ("first", "second", etc) to a numeric value. After that it adds the file names to the SortedList and then prints them out. Easy as that. If you need other FileInfo about those files, it would be very easy to add. This'll get you started. The number one thing about this is, GET LINQPAD. It is a great tool.
void Main()
{
string [] allFiles = Directory.GetFiles(@"C:\","*.txt");
SortedList allFileNames = new SortedList();
order fileSortOrder = new order();
foreach (string filename in allFiles)
{
string tempName = Path.GetFileNameWithoutExtension(filename);
allFileNames.Add(Enum.Parse(fileSortOrder.GetType(),tempName), Path.GetFileName(filename));
}
for (int i=0; i< allFileNames.Count;i++)
{
Console.WriteLine(allFileNames.GetByIndex(i));
}
}enum order
{
first,
second,
third,
fourth,
fifth,
sixth,
seventh,
eighth,
ninth,
tenth,
eleventh
}OUTPUT
first.txt
second.txt
third.txt
fourth.txt
fifth.txt
sixth.txt
seventh.txt
eighth.txt
ninth.txt
tenth.txt
eleventh.txtTrust a c#er to come up with a needlessly complex solution. Create a file: list.bat with the following content
echo first.txt
echo second.txt
echo third.txt
echo fourth.txt
echo fifth.txt
echo sixth.txt
echo seventh.txt
echo eighth.txt
echo ninth.txt
echo tenth.txt
echo eleventh.txtQED (Quite Easily Done)! You don't even need a programmer if they create more files - just a text editor. Sorted - I'll go and help Griff make coffee now (poor old sod needs help!)
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
-
Trust a c#er to come up with a needlessly complex solution. Create a file: list.bat with the following content
echo first.txt
echo second.txt
echo third.txt
echo fourth.txt
echo fifth.txt
echo sixth.txt
echo seventh.txt
echo eighth.txt
echo ninth.txt
echo tenth.txt
echo eleventh.txtQED (Quite Easily Done)! You don't even need a programmer if they create more files - just a text editor. Sorted - I'll go and help Griff make coffee now (poor old sod needs help!)
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
But...I love needless complexity. My coffee makes itself with C#. :rolleyes:
-
But...I love needless complexity. My coffee makes itself with C#. :rolleyes:
That reminds me of my first Oracle night school lesson. The guy next to me was doing something excessively complicated, so I said (genuinely not understanding) "Why do it that way instead of x?" He replied "Because I can!" The lecturer then made some disparaging remarks about programmers!
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
-
That reminds me of my first Oracle night school lesson. The guy next to me was doing something excessively complicated, so I said (genuinely not understanding) "Why do it that way instead of x?" He replied "Because I can!" The lecturer then made some disparaging remarks about programmers!
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
PhilLenoir wrote:
lecturer then made some disparaging remarks about programmers!
Programmers cannot be trusted!!! Programmers are idiots of the second kind!!! If it were up to me, I'd ban all programming. Google Is Stupid, Of Course Also, consider the stupidity of http://google.com. Why would I want to search all of those web pages? I do not! What I want is the ONE web page that answers my EXACT question. Stupid programmers. ;P Driverless Cars? Question: What is Google working on now? Answer: driverless cars. Utterly stupid. What I really want is to get to the place I am going. So get me there already. Figure that out and we don't need no stinking programmers. In the meantime, I guess we'll have to put up with them.
-
PhilLenoir wrote:
lecturer then made some disparaging remarks about programmers!
Programmers cannot be trusted!!! Programmers are idiots of the second kind!!! If it were up to me, I'd ban all programming. Google Is Stupid, Of Course Also, consider the stupidity of http://google.com. Why would I want to search all of those web pages? I do not! What I want is the ONE web page that answers my EXACT question. Stupid programmers. ;P Driverless Cars? Question: What is Google working on now? Answer: driverless cars. Utterly stupid. What I really want is to get to the place I am going. So get me there already. Figure that out and we don't need no stinking programmers. In the meantime, I guess we'll have to put up with them.
I retire in 8 weeks, so please wait until then! :)
Life is like a s**t sandwich; the more bread you have, the less s**t you eat.
-
Came across a folder having files with these names: Eighth.txt Eleventh.txt Fifth.txt First.txt Fourth.txt Ninth.txt Second.txt Seventh.txt Sixth.txt Tenth.txt Third.txt Not easy to sort :-(
Edit Feb. 13, 2015: revised linked-to file referred to here in #2. so it is internally consistent. Included sample object structure to read into by usual file-read techniques. My solution to a similar quest was: 1.Copy the ordinal table here: [^]. 2. massage it into usable form as a data file: [^]. I used the tilde (~) as the in-row item delimiter so integers could be used with commas (as in 10,000), the usual CR/LF as the row delimiter. Could probably use some more massaging. 3. parse the data file into a custom Collection after reading it, filter out what I wanted, then serialize the filtered result it for future use. The collection can be defined as simply as:
// required
using System.Collections.Generic;public class OrdinalData : List
{
// removed because the code belongs to a client
}// string numberName
// integer number
// string roman numeral
// string ordinal number
// string classifier
// example format: one~1~I~first~1st
public class OrdinalNumberData
{
public string NumberName { set; get; }
public int Number { set; get; }
public string RomanNumeral { set; get; }
public string OrdinalNumber { set; get; }
public string Classifier { set; get; }
}«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
-
Edit Feb. 13, 2015: revised linked-to file referred to here in #2. so it is internally consistent. Included sample object structure to read into by usual file-read techniques. My solution to a similar quest was: 1.Copy the ordinal table here: [^]. 2. massage it into usable form as a data file: [^]. I used the tilde (~) as the in-row item delimiter so integers could be used with commas (as in 10,000), the usual CR/LF as the row delimiter. Could probably use some more massaging. 3. parse the data file into a custom Collection after reading it, filter out what I wanted, then serialize the filtered result it for future use. The collection can be defined as simply as:
// required
using System.Collections.Generic;public class OrdinalData : List
{
// removed because the code belongs to a client
}// string numberName
// integer number
// string roman numeral
// string ordinal number
// string classifier
// example format: one~1~I~first~1st
public class OrdinalNumberData
{
public string NumberName { set; get; }
public int Number { set; get; }
public string RomanNumeral { set; get; }
public string OrdinalNumber { set; get; }
public string Classifier { set; get; }
}«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
Wow. 5 from me.
-
Solves for the known set, but what about twelfth.txt, thirteenth.txt, etc?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Start with this little beauty:
string[] nos = {"", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth",
"tenth", "eleventh", "twelfth", "thirteeth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth",
"twenty", "twenty first", "twenty second", "twenty third", "twenty fourth", "twenty fifth", "twenty sixth", "twenty seventh", "twenty eigth", "twenty ninth",
"thirty", "thirty first", "thirty second", "thirty third", "thirty fourth", "thirty fifth", "thirty sixth", "thirty seventh", "thirty eigth", "thirty ninth",
"forty", "forty first", "forty second", "forty third", "forty fourth", "forty fifth", "forty sixth", "forty seventh", "forty eigth", "forty ninth",
"fifty", "fifty first", "fifty second", "fifty third", "fifty fourth", "fifty fifth", "fifty sixth", "fifty seventh", "fifty eigth", "fifty ninth",
"sixty", "sixty first", "sixty second", "sixty third", "sixty fourth", "sixty fifth", "sixty sixth", "sixty seventh", "sixty eigth", "sixty ninth",
"seventy", "seventy first", "seventy second", "seventy third", "seventy fourth", "seventy fifth", "seventy sixth", "seventy seventh", "seventy eigth", "seventy ninth",
"eighty", "eighty first", "eighty second", "eighty third", "eighty fourth", "eighty fifth", "eighty sixth", "eighty seventh", "eighty eigth", "eighty ninth",
"ninety", "ninety first", "ninety second", "ninety third", "ninety fourth", "ninety fifth", "ninety sixth", "ninety seventh", "ninety eigth", "ninety ninth"
};(Note that you'll have to misspell some of the file names to match... :sigh: ) http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?fid=392254&select=4929745#xx4929745xx[^]
-
Wow. 5 from me.
koy baath nahin :)
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
-
Came across a folder having files with these names: Eighth.txt Eleventh.txt Fifth.txt First.txt Fourth.txt Ninth.txt Second.txt Seventh.txt Sixth.txt Tenth.txt Third.txt Not easy to sort :-(
And how about implementing
IComparer
and using that custom comparer? Then your compare implementation will need to 'translate' words to ints and then just compare the ints?!?!? Sounds plausible? Check this: http://stackoverflow.com/questions/11278081/convert-words-string-to-int[^] and [^] Cheers“The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser men so full of doubts.”
-
Came across a folder having files with these names: Eighth.txt Eleventh.txt Fifth.txt First.txt Fourth.txt Ninth.txt Second.txt Seventh.txt Sixth.txt Tenth.txt Third.txt Not easy to sort :-(
It's not that difficult. Be a programmer fer crap sake:
public class IntList : List
{
public decimal NumericValue
{
get
{
DoMath(100);
DoMath(1000);
DoMath(1000000);
DoMath(1000000000);
DoMath(1000000000000);
decimal value = 0;
foreach(decimal item in this)
{
value += item;
}
return value;
}
}private void DoMath(decimal value) { int index = this.IndexOf(value); if (index >= 1) { value \*= this\[index-1\]; this\[index\] = value; this\[index - 1\] = 0; } } public int IndexOf(decimal value) { int index = -1; for (int i = 0; i < this.Count; i++) { if (this\[i\] == value) { index = i; break; } } return index; } } public static class NumberTranslator { private static IntList values; public static Dictionary numbers = new Dictionary() { {"ZERO", 0}, {"FIRST", 1}, {"ONE", 1}, {"SECOND", 2}, {"TWO", 2}, {"THIRD", 3}, {"THREE", 3}, {"FOUR", 4}, {"FIF", 5}, {"FIVE", 5}, {"SIX", 6}, {"SEVEN", 7}, {"EIGH", 8}, {"NINE", 9}, {"TEN", 10}, {"ELEVEN", 11}, {"TWELF", 12}, {"TWELVE", 12}, {"THIRTEEN", 13}, {"FOURTEEN", 14}, {"FIFTEEN", 15}, {"SIXTEEN", 16}, {"SEVENTEEN", 17}, {"EIGHTEEN", 18}, {"NINETEEN", 19}, {"TWENTY", 20}, {"THIRTY", 30}, {"FOURTY", 40}, {"FIFTY", 50}, {"SIXTY", 60}, {"SEVENTY", 70}, {"EIGHTY", 80}, {"NINETY", 90}, {"HUNDRED", 100}, {"THOUSAND", 1000}, {"MILLION", 1000000}, {"BILLION", 1000000000} }; public static decimal Translate(string text)
-
Edit Feb. 13, 2015: revised linked-to file referred to here in #2. so it is internally consistent. Included sample object structure to read into by usual file-read techniques. My solution to a similar quest was: 1.Copy the ordinal table here: [^]. 2. massage it into usable form as a data file: [^]. I used the tilde (~) as the in-row item delimiter so integers could be used with commas (as in 10,000), the usual CR/LF as the row delimiter. Could probably use some more massaging. 3. parse the data file into a custom Collection after reading it, filter out what I wanted, then serialize the filtered result it for future use. The collection can be defined as simply as:
// required
using System.Collections.Generic;public class OrdinalData : List
{
// removed because the code belongs to a client
}// string numberName
// integer number
// string roman numeral
// string ordinal number
// string classifier
// example format: one~1~I~first~1st
public class OrdinalNumberData
{
public string NumberName { set; get; }
public int Number { set; get; }
public string RomanNumeral { set; get; }
public string OrdinalNumber { set; get; }
public string Classifier { set; get; }
}«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
Check out my solution - all code. :) http://www.codeproject.com/Messages/5000440/Re-Difficult-to-sort.aspx[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Start with this little beauty:
string[] nos = {"", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth",
"tenth", "eleventh", "twelfth", "thirteeth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth",
"twenty", "twenty first", "twenty second", "twenty third", "twenty fourth", "twenty fifth", "twenty sixth", "twenty seventh", "twenty eigth", "twenty ninth",
"thirty", "thirty first", "thirty second", "thirty third", "thirty fourth", "thirty fifth", "thirty sixth", "thirty seventh", "thirty eigth", "thirty ninth",
"forty", "forty first", "forty second", "forty third", "forty fourth", "forty fifth", "forty sixth", "forty seventh", "forty eigth", "forty ninth",
"fifty", "fifty first", "fifty second", "fifty third", "fifty fourth", "fifty fifth", "fifty sixth", "fifty seventh", "fifty eigth", "fifty ninth",
"sixty", "sixty first", "sixty second", "sixty third", "sixty fourth", "sixty fifth", "sixty sixth", "sixty seventh", "sixty eigth", "sixty ninth",
"seventy", "seventy first", "seventy second", "seventy third", "seventy fourth", "seventy fifth", "seventy sixth", "seventy seventh", "seventy eigth", "seventy ninth",
"eighty", "eighty first", "eighty second", "eighty third", "eighty fourth", "eighty fifth", "eighty sixth", "eighty seventh", "eighty eigth", "eighty ninth",
"ninety", "ninety first", "ninety second", "ninety third", "ninety fourth", "ninety fifth", "ninety sixth", "ninety seventh", "ninety eigth", "ninety ninth"
};(Note that you'll have to misspell some of the file names to match... :sigh: ) http://www.codeproject.com/Feature/WeirdAndWonderful.aspx?fid=392254&select=4929745#xx4929745xx[^]
Don't see anything in the link, but I think you have earned the rest of the day off :laugh:
-
It's not that difficult. Be a programmer fer crap sake:
public class IntList : List
{
public decimal NumericValue
{
get
{
DoMath(100);
DoMath(1000);
DoMath(1000000);
DoMath(1000000000);
DoMath(1000000000000);
decimal value = 0;
foreach(decimal item in this)
{
value += item;
}
return value;
}
}private void DoMath(decimal value) { int index = this.IndexOf(value); if (index >= 1) { value \*= this\[index-1\]; this\[index\] = value; this\[index - 1\] = 0; } } public int IndexOf(decimal value) { int index = -1; for (int i = 0; i < this.Count; i++) { if (this\[i\] == value) { index = i; break; } } return index; } } public static class NumberTranslator { private static IntList values; public static Dictionary numbers = new Dictionary() { {"ZERO", 0}, {"FIRST", 1}, {"ONE", 1}, {"SECOND", 2}, {"TWO", 2}, {"THIRD", 3}, {"THREE", 3}, {"FOUR", 4}, {"FIF", 5}, {"FIVE", 5}, {"SIX", 6}, {"SEVEN", 7}, {"EIGH", 8}, {"NINE", 9}, {"TEN", 10}, {"ELEVEN", 11}, {"TWELF", 12}, {"TWELVE", 12}, {"THIRTEEN", 13}, {"FOURTEEN", 14}, {"FIFTEEN", 15}, {"SIXTEEN", 16}, {"SEVENTEEN", 17}, {"EIGHTEEN", 18}, {"NINETEEN", 19}, {"TWENTY", 20}, {"THIRTY", 30}, {"FOURTY", 40}, {"FIFTY", 50}, {"SIXTY", 60}, {"SEVENTY", 70}, {"EIGHTY", 80}, {"NINETY", 90}, {"HUNDRED", 100}, {"THOUSAND", 1000}, {"MILLION", 1000000}, {"BILLION", 1000000000} }; public static decimal Translate(string text)
but if they were a programmer then this would be a programming question and they would be crucified for posting here usually by you lol
You cant outrun the world, but there is no harm in getting a head start Real stupidity beats artificial intelligence every time.
-
but if they were a programmer then this would be a programming question and they would be crucified for posting here usually by you lol
You cant outrun the world, but there is no harm in getting a head start Real stupidity beats artificial intelligence every time.
I honestly can't recall the last time I crucified someone for posting a programming question in the lounge. Hell, I've barely been on the site since 2012...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Check out my solution - all code. :) http://www.codeproject.com/Messages/5000440/Re-Difficult-to-sort.aspx[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013Why do I get a dejavu feeling here? Oh yeah, Q&A :laugh:
-
Came across a folder having files with these names: Eighth.txt Eleventh.txt Fifth.txt First.txt Fourth.txt Ninth.txt Second.txt Seventh.txt Sixth.txt Tenth.txt Third.txt Not easy to sort :-(
I wrote an article (and the code I previously posted in this thread is broken). The code in the aticle works much better. Converting Text Numbers to Numeric Values[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I honestly can't recall the last time I crucified someone for posting a programming question in the lounge. Hell, I've barely been on the site since 2012...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013yes but the reputation remains
You cant outrun the world, but there is no harm in getting a head start Real stupidity beats artificial intelligence every time.
-
yes but the reputation remains
You cant outrun the world, but there is no harm in getting a head start Real stupidity beats artificial intelligence every time.
Bergholt Stuttley Johnson wrote:
but the reputation remains
Without Enkidu there would be no Gilgamesh Epic, Without Sancho Panza no Quixote, without Satan, no Paradise Lost, etc. If we didn't have a need for an Eastwood/Norris figure, we wouldn't create one; of course, the protagonist, in this case, is self-recruited for the part. I think JSOP is doing a bang-up job, and his technical-foo is satisfyingly killer :) cheers, Bill
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009