473,811 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Arraylist

I need to sort an ArrayList of objects. I am unable to find a method
for this. DO I need to role my own sorting? Or is there something like
the qsort() function in C of old???

Thanks
Nov 16 '05 #1
6 8190
Hi,
You can use Sort () method. (ArrayList arr = new ArrayList (); arr.Sort
(); )

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Bryon" <bl***@whittman hart.com> je napisal v sporocilo
news:ep******** ******@TK2MSFTN GP14.phx.gbl ...
I need to sort an ArrayList of objects. I am unable to find a method for
this. DO I need to role my own sorting? Or is there something like the
qsort() function in C of old???

Thanks

Nov 16 '05 #2
Bryon <bl***@whittman hart.com> wrote:
I need to sort an ArrayList of objects. I am unable to find a method
for this. DO I need to role my own sorting? Or is there something like
the qsort() function in C of old???


Depending on how you want to sort the ArrayList you can use the Sort()
method of the ArrayList class, if you want to sort by something other
than the 'value' of the Objects you may need to implement an IComparer
that does the comparisons on the appropriate values derived from the
objects - see my post about sorting files by creation date earlier.

/J\
Nov 16 '05 #3
Bryon,

Why not use a sortedlist than instead of the arraylist?

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor
Nov 16 '05 #4
Cor Ligthert <no************ @planet.nl> wrote:
Why not use a sortedlist than instead of the arraylist?

http://msdn.microsoft.com/library/de...ary/en-us/cpre
f/html/frlrfsystemcoll ectionssortedli stclasstopic.as p

I hope this helps?


If the OP really wants a "normal" list, SortedList is a bad choice -
it's really a map, which also keeps the keys sorted. It's very badly
named, unfortunately.

Fortunately, the Sort method on ArrayList is just what the OP wants, I
believe :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
>
Fortunately, the Sort method on ArrayList is just what the OP wants, I
believe :)


You can be right, however the OP is writing an Arraylist of objects.

I do not know if those objects contain a single value. I don't think so
because he said he was not able to find a method. When you search on MSDN
with "arraylist sort" you get direct the arraylist sort method.

So I think there should me something more.

However I can be wrong in this.

Cor

Nov 16 '05 #6
Cor Ligthert <no************ @planet.nl> wrote:
Fortunately, the Sort method on ArrayList is just what the OP wants, I
believe :)
You can be right, however the OP is writing an Arraylist of objects.

I do not know if those objects contain a single value.


Why would it matter?
I don't think so
because he said he was not able to find a method. When you search on MSDN
with "arraylist sort" you get direct the arraylist sort method.


It's sometimes easy to miss the obvious...

<snip>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1723
by: awiklund | last post by:
Hi, I am sorting an arrayList using a custom comparer, but it sort the element in a uncommon fashion. Take a look at this code. using System; namespace ConsoleApplication8
2
5076
by: Rob G | last post by:
Hello, I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out. I want to sort a CheckBoxList. Using the ArrayList seemed to make the most sense. When I use it I get the error: At least one object must implement IComparable. Here's the code that produces that error: Public Class Sorting
19
25476
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to sort columns based on the column header the user has clicked in both Ascending and Descending formats.
16
6434
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
2
4731
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web page to display this data which I have done, however, now I would like to sort it based on a data item/direction selected by the user from the web page.
16
2769
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this: sort index, id 1000,1 1000,2 1000,3 1001,1 1001,2
3
3129
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but unfortunately in opposite to the example from this link here it does not sort. I Create Nodes ( color, name ) and add them to nodeList in Graph class. Then i try to sort.. The code is relatively simple:
12
9794
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath As String End Structure
4
5329
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file (Sorts.java): import java.util.*; /**
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.