Friday, February 6, 2009

Sorting 2 dimensional array/arraylist

See code snippet for an example. Let me know if you have a question.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// Holds the list from the file
List<String> dataSorted = new List<string>();
// Read the lines from the file
StreamReader sr = new StreamReader("Data.txt");
while (!sr.EndOfStream)
{
// insert line into list
dataSorted
.Add(sr.ReadLine());
}
// Sort the list with a custom compare
dataSorted
.Sort(AddressCompare);

// Display the list to the console window or write to file
foreach( String data in dataSorted )
{
Console.WriteLine(data);
}
}

int AddressCompare(object x, object y)
{
// Two null objects are equal
if (x == null && y == null) return 0;
//Any object that is not null is greater then an object that is null
if (x == null) return -1;
if (y == null) return 1;

// CheckBox to see that both objects are Strings
if (!(x.GetType() == typeof(String) && y.GetType() == typeof(String)))
{
throw new ArgumentException("Can't compare " + x.GetType().ToString() +
"/" + y.GetType().ToString());
}

// Parse the string to get the address
String[] addr = new String[2];
String[] type = new String[2];
type
[0] = x.ToString().Substring(0, 2);
type
[1] = y.ToString().Substring(0, 2);
for (int idx = 0; idx < 2; idx++)
{
// Not sure how long the address field is so I added all the types given in the
// you will need to modify the length of the address for each type element 0 is
// the x string and element 1 is the y string.
switch (type[idx])
{
case "S0":
if (idx == 0)
{
addr
[0] = x.ToString().Substring(4, 6);
}
else
{
addr
[1] = y.ToString().Substring(4, 6);
}
break;
case "S3":
if (idx == 0)
{
addr
[0] = x.ToString().Substring(4, 6);
}
else
{
addr
[1] = y.ToString().Substring(4, 6);
}
break;
case "S5":
if (idx == 0)
{
addr
[0] = x.ToString().Substring(4, 6);
}
else
{
addr
[1] = y.ToString().Substring(4, 6);
}
break;
case "S7":
if (idx == 0)
{
addr
[0] = x.ToString().Substring(4, 6);
}
else
{
addr
[1] = y.ToString().Substring(4, 6);
}
break;
}
}
// Convert hex string to integer
int[] key = new int[2];
key
[0] = int.Parse(addr[0], System.Globalization.NumberStyles.HexNumber);
key
[1] = int.Parse(addr[1], System.Globalization.NumberStyles.HexNumber);

// compare integer values
if (key[0] == key[1]) return 0;
if (key[0] < key[1]) return -1;
return 1;
}
}
}

No comments:

Post a Comment