Click here to Skip to main content
15,889,909 members
Home / Discussions / C#
   

C#

 
AnswerRe: How To Read And Write OMRON PLC? Pin
tiliga16-Oct-13 17:00
tiliga16-Oct-13 17:00 
GeneralRe: How To Read And Write OMRON PLC? Pin
Dave Kreskowiak17-Oct-13 1:17
mveDave Kreskowiak17-Oct-13 1:17 
AnswerRe: How To Read And Write OMRON PLC? Pin
w1dge2-Nov-13 11:46
w1dge2-Nov-13 11:46 
QuestionSorted Collection in C# Pin
Member 1033982916-Oct-13 4:34
Member 1033982916-Oct-13 4:34 
AnswerRe: Sorted Collection in C# Pin
Richard MacCutchan16-Oct-13 5:26
mveRichard MacCutchan16-Oct-13 5:26 
GeneralRe: Sorted Collection in C# Pin
Matt T Heffron16-Oct-13 8:45
professionalMatt T Heffron16-Oct-13 8:45 
AnswerRe: Sorted Collection in C# Pin
Eddy Vluggen16-Oct-13 11:15
professionalEddy Vluggen16-Oct-13 11:15 
AnswerRe: Sorted Collection in C# Pin
BillWoodruff16-Oct-13 15:45
professionalBillWoodruff16-Oct-13 15:45 
You need a collection of ... what ? And, what is the "sorting algorithm" you require/need/want ?

.NET hands you the ability to easily sort (using a QuickSort algorithm) generic Lists of value types by just invoking the 'Sort function: "All numeric types (such as Int32 and Double) implement IComparable, as do String, Char, and DateTime. Custom types should also provide their own implementation of IComparable to enable object instances to be ordered or sorted."

And, writing your own IComparable is not that difficult; for example:

If you have a TreeNode Collection:
C#
public List<TreeNode> sTNList = new List<TreeNode>
{
    new TreeNode("1"), 
    new TreeNode("1"), 
    new TreeNode("2"), 
    new TreeNode("4"), 
    new TreeNode("1"), 
    new TreeNode("4"), 
    new TreeNode("8"), 
    new TreeNode("2"), 
    new TreeNode("3") 
};
You can easily define a custom IComparer Class to sort the nodes:
public class TreeNodeICompare : IComparer<TreeNode>
{
    public int Compare(TreeNode x, TreeNode y) 
    {
        // basic alphanumeric sort     
        return(x.Text.CompareTo(y.Text));
    } 
}
And invoke it:
sTNList.Sort(0, sTNList.Count, new TreeNodeICompare());
See [^] for the documentation on the call to 'Sort used here. And, note: "This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. ... On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation."

Using a non-unique property of a collection, like the 'Text property of a TreeNode, you avoid possible duplicate key issues that might arise with other types of collections.

Perhaps if you describe the scenario in which your collection is being used you can get more than a "mind-reader's guess" type of response here.

bill

Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview


modified 16-Oct-13 22:12pm.

AnswerRe: Sorted Collection in C# Pin
NWPU_Tbeck16-Oct-13 17:04
NWPU_Tbeck16-Oct-13 17:04 
AnswerRe: Sorted Collection in C# Pin
Abhinav S16-Oct-13 18:22
Abhinav S16-Oct-13 18:22 
AnswerRe: Sorted Collection in C# Pin
Dinkar Tiwari16-Oct-13 21:25
Dinkar Tiwari16-Oct-13 21:25 
AnswerRe: Sorted Collection in C# Pin
Gabriel Szabo17-Oct-13 1:22
Gabriel Szabo17-Oct-13 1:22 
QuestionCapture image from USB 2.0 PC Camera using physical button in the camera Pin
Member 794843016-Oct-13 2:10
Member 794843016-Oct-13 2:10 
AnswerRe: Capture image from USB 2.0 PC Camera using physical button in the camera Pin
Eddy Vluggen16-Oct-13 11:19
professionalEddy Vluggen16-Oct-13 11:19 
GeneralRe: Capture image from USB 2.0 PC Camera using physical button in the camera Pin
Member 794843016-Oct-13 15:05
Member 794843016-Oct-13 15:05 
GeneralRe: Capture image from USB 2.0 PC Camera using physical button in the camera Pin
Dave Kreskowiak17-Oct-13 1:19
mveDave Kreskowiak17-Oct-13 1:19 
Questionweb site interaction Pin
Member 1032069915-Oct-13 10:35
Member 1032069915-Oct-13 10:35 
AnswerRe: web site interaction Pin
Bernhard Hiller15-Oct-13 21:13
Bernhard Hiller15-Oct-13 21:13 
QuestionIn Console Application design table with background column dynamically Pin
karthikchinnu15-Oct-13 7:20
karthikchinnu15-Oct-13 7:20 
SuggestionRe: In Console Application design table with background column dynamically Pin
Richard MacCutchan15-Oct-13 21:35
mveRichard MacCutchan15-Oct-13 21:35 
QuestionAnyone work with AutoCad files? Pin
SledgeHammer0115-Oct-13 4:47
SledgeHammer0115-Oct-13 4:47 
AnswerRe: Anyone work with AutoCad files? Pin
Eddy Vluggen15-Oct-13 5:02
professionalEddy Vluggen15-Oct-13 5:02 
GeneralRe: Anyone work with AutoCad files? Pin
SledgeHammer0115-Oct-13 5:19
SledgeHammer0115-Oct-13 5:19 
QuestionRe: Anyone work with AutoCad files? Pin
Eddy Vluggen15-Oct-13 7:00
professionalEddy Vluggen15-Oct-13 7:00 
AnswerRe: Anyone work with AutoCad files? Pin
TnTinMn15-Oct-13 18:24
TnTinMn15-Oct-13 18:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.