Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 6 integers (a, b, c, d, e ,f) and they have random values. i want to sort them according to their values.
i used arraylist but after sorting it sorts according to 1st digit of integer. ie.. if sorting among (10, 15,8 and 23 )
output is 10,15,23,8
how to get correct output what i m missing, please help me out
Posted
Updated 1-Oct-11 21:19pm
v2
Comments
Bala Selvanayagam 1-Oct-11 16:22pm    
Indiana.Net..Is it possible for to post your code trying to sort the integers so that it will be easier for me to see where the issue is please
Alan N 2-Oct-11 11:04am    
The output order 10,15,23,8 must have been obtained by sorting strings instead of integers. If you had posted your code as requested by Bala I think you would have had an answer very quickly.

use Generic list of integer than try to sort them.
 
Share this answer
 
v2
Comments
Sander Rossel 2-Oct-11 4:12am    
My 5. This seems the easiest way to sort Integers. No need to create your own sorting algorithms or box the ints into Objects...

List<int> myList = new List<int>();
myList.AddRange(a, b, c, d, e, f);
myList.Sort();

From the top of my head :)
Simon Bang Terkildsen 3-Oct-11 9:47am    
No need to create your own sorting algorithms
Insight is one reason and a very good one at that :)
Sander Rossel 3-Oct-11 10:35am    
Insight of what? Microsoft has written some integer sorting algorithm for us already. No need to write your own. Writing your own only means more code to maintain.
Even for sorting custom classes I'd use List.Sort(New MyIComparer). By far the easiest way and possibly faster then anything I'd ever write since I'm no algorithm expert, and I think I'm not the only one :)
Simon Bang Terkildsen 3-Oct-11 10:41am    
Insight into how a sorting algorithm works, which might be a good idea if you want to become a software engineer, developer, programmer or what ever titles you might think of.
While I whole heartily agree that you should not write unnecessary code, that kind of thinking when it comes to academic pursuits means you wont ever become even an average programmer.
Sander Rossel 3-Oct-11 10:50am    
Learning how algorithms work is probably very useful in some cases. However, when working with .NET a lot has already been done for you and you probably won't need it. And I really don't think my boss (or any boss) would approve of me writing some custom integer sorting algorithm if it is already implemented in .NET and we've got that deadline catching in on us ;)
So great for learning and curiosity, but never ever in production code (not for integers in .NET anyway).
plenty of sorting algorithms here[^], make your pick
 
Share this answer
 
Comments
choudhary.sumit 1-Oct-11 15:59pm    
i need any c# code to accomplish, i really cant do this by reading definition.
Simon Bang Terkildsen 1-Oct-11 16:04pm    
Most if not all of them have their own page with a pseodocode implementation.

I'm not just gonna hand you an implementation, you've shown no effort to resolve your problem, if you want to be able to post a problem and get a complete solution then there are plenty of sites that allow you to post this and pay any developer willing to do the work for you.
An example for this:
C#
int sortObj(object obj1, object obj2)
{
    return (int)obj1 == (int)obj2 ? 0 : (int)obj1 > (int)obj2 ? 1 : -1;
}

private void Form1_Load(object sender, EventArgs e)
{
    int a=9, b=2, c=5, d=1;

    object[] obj = new object[4]{a,b,c,d};

    Array.Sort<object>(obj, new Comparison<object>(sortObj));
}
 
Share this answer
 
you can try Generic list of integer than you can easly sort them
 
Share this answer
 
Don't use an ArrayList! Always use one of the generic lists instead - normally List<T>.

If it's only integers in the list then use List<int>. you can then call it's Sort method which will use int's built in CompareTo method to sort them as you want.

If ever you need to hold a list of different types (and there isn't another suitable common base type) use List<object>. There is no reason to use ArrayList unless you are using or need to be compatible with .NET 1.0 or 1.1 code which is highly unlikely.
 
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