473,757 Members | 7,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array find and sort

Hi,

I need to add this inside an array:
1 3
2 4
3 2
4 5
5 1

I think of using this:
Int32[,] myValues;

Now I want to find a value (index) in the first colomn. Let's say I'm
looking for value 3, the find result will be 3.
After that I want to sort the array bases on the values in column two.

How can I do that?

Thanks!

Nov 17 '05 #1
4 2197
Whoops...

"...result will be 3." must be "...result will be 2."

And "...array bases on..." should be "...array based on..."
Nov 17 '05 #2

Arjen wrote:
Whoops...

"...result will be 3." must be "...result will be 2."

And "...array bases on..." should be "...array based on..."


You are looking for a map implementation. There are lots of them. A
simplistic approach might be...

public class Map
{
public int _key;
public int _val;

public Map(int key, int val)
{
_key = key;
_val = val;
}

public bool Match( int val )
{
return _key == val;
}
}

// Then to add and search...
ArrayList al = new ArrayList();
al.Add( new Map(1,3) );
al.Add( new Map(2,4) );
al.Add( new Map(3,2) );

// Find an entry for 3.
int retVal = -1;
for ( int i=0; i<al.Count; ++i )
{
Map m = (Map)al[i];
if ( m.Match( 3 ) )
{
retVal = m._val;
break;
}
}

Nov 17 '05 #3
What you want to do is use a Hashtable to store a key value.

I didnt test it.
But I don't see why it shouldn't work.

public struct KeyValue
{
public KeyValue(int key, int value)
{
Key = key;
Value = value;
}

public int Key;
public int Value;
}

public class MyComparer : IComparer
{
int IComparer.Compa re(Object x, Object y)
{
KeyValue keyValueLeft = (KeyValue) x;
KeyValue keyValueRight = (KeyValue) y;
return((new CaseInsensitive Comparer()).Com pare(keyValueRi ght.Value,
keyValueLeft.Va lue));
}
}

public void test()
{
ArrayList array = new ArrayList();
array.Add(new KeyValue(1,3));
array.Add(new KeyValue(2,4));
array.Add(new KeyValue(3,2));
array.Add(new KeyValue(4,5));
array.Add(new KeyValue(5,1));
array.Sort(new MyComparer());

Hashtable table = new Hashtable();
foreach(KeyValu e keyValue in array)
table.Add(keyVa lue.Key, keyValue.Value) ;

//Here we look for keys....
Console.WriteLi ne(table[3]); // the return object is "2";

//The hastable was already loaded with sorted Key Values by the second
colum.
foreach(int i in table.Keys)
Console.WriteLi ne("Key:" + i + " Value:" + table[i]);

//It should shows
//Key: 5 Value: 1
//Key: 3 Value: 2
//Key: 1 Value: 3
//Key: 2 Value: 4
//Key: 4 Value: 5
}

Gustavo.

"Arjen" <bo*****@hotmai l.com> wrote in message
news:dc******** **@news4.zwoll1 .ov.home.nl...
Hi,

I need to add this inside an array:
1 3
2 4
3 2
4 5
5 1

I think of using this:
Int32[,] myValues;

Now I want to find a value (index) in the first colomn. Let's say I'm
looking for value 3, the find result will be 3.
After that I want to sort the array bases on the values in column two.

How can I do that?

Thanks!

Nov 17 '05 #4
this is a more elegant solution:

try this and let me know if it works.

public class MyComparer2 : IComparer
{
private SortedList mSortedList;

public SortedList SortedList
{
get{return mSortedList;}
set{mSortedList = value;}
}

int IComparer.Compa re(Object x, Object y)
{
int keyLeft = (int) x;
int keyRight = (int) y;
return((new CaseInsensitive Comparer()).Com pare(mSortedLis t[y],
mSortedList[x]));
}
}

public void test()
{
MyComparer2 comparer = new MyComparer2();
SortedList sortedList = new SortedList(comp arer);
comparer.Sorted List = sortedList;

sortedList.Add( 1,3);
sortedList.Add( 2,4);
sortedList.Add( 3,2);
sortedList.Add( 4,5);
sortedList.Add( 5,1);

//Here we look for keys....
Console.WriteLi ne(sortedList[3]); // the return object is "2";

//The SortedList was dinamically sorted by the second column when the items
are loaded.
foreach(int i in sortedList.Keys )
Console.WriteLi ne("Index:" + sortedList.Inde xOfKey(i) + " Key:" + i + "
Value:" + sortedList[i]);

//It should shows
//Index:1 Key: 5 Value: 1
//Index:2 Key: 3 Value: 2
//Index:3 Key: 1 Value: 3
//Index:4 Key: 2 Value: 4
//Index:5 Key: 4 Value: 5
}

Gustavo

"Franco, Gustavo" <gustavo_fran co[REMOVEIT]@hotmail.com> wrote in message
news:ON******** ******@TK2MSFTN GP09.phx.gbl...
What you want to do is use a Hashtable to store a key value.

I didnt test it.
But I don't see why it shouldn't work.

public struct KeyValue
{
public KeyValue(int key, int value)
{
Key = key;
Value = value;
}

public int Key;
public int Value;
}

public class MyComparer : IComparer
{
int IComparer.Compa re(Object x, Object y)
{
KeyValue keyValueLeft = (KeyValue) x;
KeyValue keyValueRight = (KeyValue) y;
return((new CaseInsensitive Comparer()).Com pare(keyValueRi ght.Value,
keyValueLeft.Va lue));
}
}

public void test()
{
ArrayList array = new ArrayList();
array.Add(new KeyValue(1,3));
array.Add(new KeyValue(2,4));
array.Add(new KeyValue(3,2));
array.Add(new KeyValue(4,5));
array.Add(new KeyValue(5,1));
array.Sort(new MyComparer());

Hashtable table = new Hashtable();
foreach(KeyValu e keyValue in array)
table.Add(keyVa lue.Key, keyValue.Value) ;

//Here we look for keys....
Console.WriteLi ne(table[3]); // the return object is "2";

//The hastable was already loaded with sorted Key Values by the second
colum.
foreach(int i in table.Keys)
Console.WriteLi ne("Key:" + i + " Value:" + table[i]);

//It should shows
//Key: 5 Value: 1
//Key: 3 Value: 2
//Key: 1 Value: 3
//Key: 2 Value: 4
//Key: 4 Value: 5
}

Gustavo.

"Arjen" <bo*****@hotmai l.com> wrote in message
news:dc******** **@news4.zwoll1 .ov.home.nl...
Hi,

I need to add this inside an array:
1 3
2 4
3 2
4 5
5 1

I think of using this:
Int32[,] myValues;

Now I want to find a value (index) in the first colomn. Let's say I'm
looking for value 3, the find result will be 3.
After that I want to sort the array bases on the values in column two.

How can I do that?

Thanks!


Nov 17 '05 #5

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

Similar topics

11
3266
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? A.sort().slice(0,n) // would do it, but sorts; and the number
7
3266
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
2
5234
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ============================================================================
19
2631
by: David | last post by:
Hi all, A while back I asked how to sort an array of strings which would have numerals and I wanted to put them in sequential numerical order. For example: myArray = "file1"; myArray = "file2"; myArray = "file3"; myArray = "file4"; myArray = "file5";
21
3220
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
3
3738
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
4
3034
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. example array: arr
23
7416
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
3
1458
by: Richard | last post by:
How to sort an array without replacing the indices? I have an array indexed by a timestamp and wish to sort based on that in numeric ascending order. thanks for any advice.
0
9489
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
10072
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
9906
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
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.