Hi,
I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this?
I've coded some example here... -
ArrayList arr = new ArrayList();
-
Hashtable row = new Hashtable();
-
Hashtable row2 = new Hashtable();
-
row.Add("name", "Naomi Watts");
-
row.Add("age", "24");
-
row2.Add("name", "Scarlett Johanson");
-
row2.Add("age","16");
-
arr.Add(row);
-
arr.Add(row2);
-
Now consider that I want to sort the ArrayList ascending on row["age"].
Sorting on IComparer works only when sorting on Objects, and I can't find a way for this situation.
11 4768
You should be typecasting the object back to a hash table type
cheers.
PS: your structure dosent look so good!
How can I do that? Simply by "(Hashtable)..." or "as Hashtable"? Can you give an example please? And whats wrong with the structure? How would you do it in a situation where you have to save columns and rows?
IComparer interface has two objects come in, that you then type-cast to the object for sorting purposes.
As for your structure, there seems to be a lot of problems.
you are only using ONE hastable, never creating another one. So your arraylist is just going to have the same hastable in it everytime.
It rather seems that a DataTable would be more useful to you in this situation (define some colums, add values in a row) So instead of a new hashtable each time, you would just add another row to your DataTable.
Yes I saw it, thanks...It was only a sample code and in the actual source it's written right. I changed it now in the post. I could have use DataTables but me was said that this is a much faster solution :S
I will try to typecast and see if it works, thanks!
Small problem left :D don't know how to fill in the arr.Sort(...) and if it will work like this.
Thank you! -
using System;
-
using System.Collections;
-
using System.Collections.Generic;
-
using System.Text;
-
-
namespace test
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
ArrayList arr = new ArrayList();
-
Hashtable row = new Hashtable();
-
Hashtable row2 = new Hashtable();
-
row.Add("name", "Naomi Watts");
-
row.Add("age", "24");
-
row2.Add("name", "Scarlett Johanson");
-
row2.Add("age", "16");
-
arr.Add(row);
-
arr.Add(row2);
-
-
Array.Sort(); <----- How dow I fill this in?
-
}
-
}
-
-
class sortAscending : IComparer
-
{
-
int IComparer.Compare(object a, object b)
-
{
-
//Typecast the object to Hashtables
-
Hashtable c1 = (Hashtable)a;
-
Hashtable c2 = (Hashtable)b;
-
if (Int32.Parse(c1["age"].ToString()) > Int32.Parse(c2["age"].ToString()))
-
return 1;
-
if (Int32.Parse(c1["age"].ToString()) < Int32.Parse(c2["age"].ToString()))
-
return -1;
-
else
-
return 0;
-
}
-
-
public static IComparer sortAsc()
-
{
-
return (IComparer)new sortAscending();
-
}
-
-
}
-
}
-
Try this: -
Array.Sort(arr, new sortAscending());
-
You don't need that static method in your sortAscending class.
Sorry, won't work...
1.The best overloaded method match for 'System.Array.Sort(System.Array, System.Array)' has some invalid arguments \Projects\test\Program.cs 22 13 test
2.Argument '1': cannot convert from 'System.Collections.ArrayList' to 'System.Array' \Projects\test\Program.cs 22 24 test
3.Argument '2': cannot convert from 'test.sortAscending' to 'System.Array' \Projects\test\Program.cs 22 29 test
Ooooo it's mad because of that, hehe oops -
arr.Sort(new sortAscending());
-
LOL! Plater it keeps complaining :(
1.The best overloaded method match for 'System.Array.Sort(System.Array)' has some invalid arguments \Projects\test\Program.cs 22 13 test
2.Argument '1': cannot convert from 'void' to 'System.Array' \Projects\test\Program.cs 22 24 test
You put in "arr" and not "Array" before the .Sort() right?
:$ sorry ... It now works perfectly! This surely made me learn a bit about IComparer. Thank you for your help!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: renster |
last post by:
I have been looking at implementing the following but with a difference.
http://forums.devshed.com/t47653/s.html
This example uses a link which works with some code shown on the page.
I have...
|
by: Netmonster |
last post by:
Hello,
Does any one have an example of how to create an ArrayList of objects?
i.e. ArrayList of ArrayLists or an ArrayList of Hashtables?
Thanks in advance
KC
|
by: Adam J. Schaff |
last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class
does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii
values) but what I got was the opposite....
|
by: Derek Martin |
last post by:
Hi there, I have been playing with sorting my arraylist and having some
troubles. Maybe just going about it wrong. My arraylist contains objects
and one of the members of the object is 'name.' I...
|
by: Alex Chudnovsky |
last post by:
I have come across with what appears to be a significant performance bug in
..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same
data. Same data on the same CPU gets sorted a...
|
by: dwelden |
last post by:
I have successfully used the sort lambda construct described in
http://mail.python.org/pipermail/python-list/2006-April/377443.html.
However, how do I take it one step further such that some values...
|
by: shana07 |
last post by:
Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array from 12, 64, 8, 128 etc.
3 12
4 64
7 8
10 128
...
I just wanna...
|
by: Bruce |
last post by:
Hi
I am having a problem understanding the exact advantages of using an
ArrayList over a Hashtable in any situation.
In most areas of an application I am working on, lookup needs to be
fast. If I...
|
by: Kamran Shafi |
last post by:
Hi,
I am creating an arraylist (say masterArrayList) of hashtables, where
each hashtable (say table) is of the format key=string, value =
arraylist of strings (say existing_strings). In a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |