I'm sure this is an FAQ, but the first 12 items Google returned do not have this question in it. I have a SortedDictionary(of String, String) where the first item is an apartment number and the second a tenant name. I populare, then loop over the results and print, and I get something like... A-10  Bob Smith A-12  John Taylor A-1   Alice Monroe A-2   Janice O'Dell  I am at a complete loss as to what this sorting is trying to do, but in any event, I need this list to be A-1, A-2, A-10, A-12 (etc.) We are forbidden from using LINQ (above my pay grade), so is there some easy way to change the sorting to be as expected?
626
Anyone have a good resource or provide a sample of a natural order sort in C# for an FileInfo array?  I am implementing the IComparer interface in my sorts.
81019
It seems that sort behaves strangely on lines such as &gt;&gt;b    $ cat test a &gt;&gt;b b c &gt; &gt;&gt;  $ sort test &gt; &gt;&gt; a b &gt;&gt;b c   I would expect the &gt;&gt;b line to be the third line output by sort but it is the fifth. Why does it do this and is there a way to make sort give my expected output?    I'm using GNU/Linux Ubuntu 16.04.
95963
Let's say that I have two arrays (in Java),  int[] numbers; and int[] colors;  Each ith element of numbers corresponds to its ith element in colors. Ex, numbers = {4,2,1}      colors = {0x11, 0x24, 0x01}; Means that number 4 is color 0x11, number 2 is 0x24, etc.  I want to sort the numbers array, but then still have it so each element matches up with its pair in colors.  Ex. numbers = {1,2,4};     colors = {0x01,0x24,0x11};  What's the cleanest, simplest way to do this? The arrays have a few thousand items, so being in place would be best, but not required. Would it make sense to do an Arrays.sort() and a custom comparator? Using library functions as much as possible is preferable.  Note: I know the "best" solution is to make a class for the two elements and use a custom comparator. This question is meant to ask people for the quickest way to code this. Imagine being at a programming competition, you wouldn't want to be making all these extra classes, anonymous classes for the comparator, etc. Better yet, forget Java; how would you code it in C?
4557
I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.  I can sort on the keys, but how can I sort based on the values?  Note: I have read Stack Overflow question here  and probably could change my code to have a list of dictionaries, but since I do not really need a list of dictionaries I wanted to know if there is a simpler solution to sort either in ascending or descending order.
90263
I have a text column. It's made up of the numbers 1, 2, 3 up to 1650, which are currently not in increasing order. However, it also includes text such as 520a, 639a, 639b, 639c. Sorting on increasing value would produce 1, 10, 100, 1000, etc, rather than 1, 2, 3, ... I know how to convert from text to integer, which would work if my cells did not include alpha characters. How can I sort to end up with 1, 2, 3, ..., 10, ..., 100, ..., 519, 520a, 521, ..., 638, 639a, 639b, 639c, 640, ..., 1650?
37453
I have a list of tuples like this:  [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ]   I want to iterate through this keying by the first item, so, for example, I could print something like this:  a 1 2 3 b 1 2 c 1   How would I go about doing this without keeping an item to track whether the first item is the same as I loop around the tuples? This feels rather messy (plus I have to sort the list to start with)...
26779
Lets say I have an array like this:  string [] Filelist = ...   I want to create an Linq result where each entry has it's position in the array like this:  var list = from f in Filelist     select new { Index = (something), Filename = f};   Index to be 0 for the 1st item, 1 for the 2nd, etc.  What should I use for the expression Index= ?
51302
There's an existing function that ends in the following, where d is a dictionary:  return d.iteritems()   that returns an unsorted iterator for a given dictionary. I would like to return an iterator that goes through the items sorted by key. How do I do that?
41680
If I have the following string:  string s = "abcdefghab";   Then how do I get a string (or char[]) that has just the characters that are repeated in the original string using C# and LINQ. In my example I want to end up with "ab".  Although not necessary, I was trying to do this in a single line of LINQ and had so far come up with:  s.ToCharArray().OrderBy(a =&gt; a)...
28318
I'm trying to format numbers. Examples:  1     =&gt; 1 12    =&gt; 12 123   =&gt; 123 1234  =&gt; 1,234 12345 =&gt; 12,345   It strikes as a fairly common thing to do but I can't figure out which filter I'm supposed to use.  Edit: If you've a generic Python way to do this, I'm happy adding a formatted field in my model.
1512
I am looking for a generic, bidirectional 1 to 1 Dictionary class in C# (2), ie. a BiDictionaryOneToOne&lt;T, S&gt; which is guaranteed to only contain one of each value and key (up to RefEquals anyway), and which can be searched using either key or value. Anyone know of one, or should I just implement it myself? I can't believe that I'm the first person to need this...  There is a BiDictionary in the answers to , but it is not for unique elements (and also does not implement RemoveByFirst(T t) or RemoveBySecond(S s)).   Thanks!
44578
Say I have two lists:  var list1 = new int[] {1, 2, 3}; var list2 = new string[] {"a", "b", "c"};   Is it possible to write a LINQ statement that will generate the following list:  var result = new []{      new {i = 1, s = "a"},     new {i = 1, s = "b"},     new {i = 1, s = "c"},     new {i = 2, s = "a"},     new {i = 2, s = "b"},     new {i = 2, s = "c"},     new {i = 3, s = "a"},     new {i = 3, s = "b"},     new {i = 3, s = "c"} };   ?  Edit: I forgot to mention I didn't want it in query syntax. Anyway, based on preetsangha's answer I've got the following:  var result = list1.SelectMany(i =&gt;  list2.Select(s =&gt; new {i = i, s = s}));
52342
I need to randomly 'sort' a list of integers (0-1999) in the most efficient way possible.  Any ideas?  Currently, I am doing something like this:  bool[] bIndexSet = new bool[iItemCount];  for (int iCurIndex = 0; iCurIndex &lt; iItemCount; iCurIndex++) {     int iSwapIndex = random.Next(iItemCount);     if (!bIndexSet[iSwapIndex] &amp;&amp; iSwapIndex != iCurIndex)     {         int iTemp = values[iSwapIndex];         values[iSwapIndex] = values[iCurIndex];         values[iCurIndex] = values[iSwapIndex];         bIndexSet[iCurIndex] = true;         bIndexSet[iSwapIndex] = true;     } }
75761
I'd like to do the equivalent of the following in LINQ, but I can't figure out how:  IEnumerable&lt;Item&gt; items = GetItems(); items.ForEach(i =&gt; i.DoStuff());   What is the real syntax?
31225
So I have a generic list, and an oldIndex and a newIndex value.  I want to move the item at oldIndex, to newIndex...as simply as possible.  Any suggestions?  Note  The item should be end up between the items at (newIndex - 1) and newIndex before it was removed.
45508
I am aware that it is possible to sort primarily by one field and secondarily by another field. For example:   $ echo -e "1 5\n1 4\n8 2\n1 9" | sort -k 1,1n -k 2,2n 1 4 1 5 1 9 8 2 $    However, am I correct that sort always keeps the lines intact, e.g. it is not possible to sort in a way that output would be following:  1 2 1 4 1 5 8 9   If this is not possible with sort, then what would be the correct tool for this?
79673
e.g,  foo1 foo2 foo10 foo100   rather than  foo1 foo10 foo100 foo2   Update: not interested in coding the sort myself (although that's interesting in its own right), but having the database to do the sort for me.