Hi all,

I have an ArrayList of similar objects.

Now I have to split the arraylist into multiple arraylist based on a property of these objects.

Here is the sample data set
Arraylist:
A{1, A}
A{2, A}
A{3, B}
A{4, B}
A{5, B}
A{6, C}

Result should be

Arraylist 1:
A{1, A}
A{2, A}

Arraylist 2:
A{3, B}
A{4, B}
A{5, B}

Arraylist 3:
A{6, C}

Any body has a easier way doing this...

somebody help me, please ...

What ways have you already tried?

Is it necessary that it is an ArrayList?

Do you add object to array list like that:

ArrayList main = new ArrayList();
            main.Add("1,A");
            main.Add("2,A");
            main.Add("3,B");

Consider converting ArrayList to List or array.
Then you can use the Linq extension or Array class static methods.

class tupple
{
	public tupple(int number, char data) 
	{
		Number = number;
		Data = data;
	}
	public int Number { get; set; }
	public char Data { get; set; }
}

private void button5_Click(object sender, EventArgs e) 
{
	var A = new ArrayList();
	A.Add(new tupple(1, 'A'));
	A.Add(new tupple(2, 'A'));
	A.Add(new tupple(3, 'B'));
	A.Add(new tupple(4, 'B'));
	A.Add(new tupple(5, 'B'));
	A.Add(new tupple(6, 'C'));

	// convert ArrayList to list of tupple
	var Alist = A.Cast<tupple>().ToList();

	// find objects with Data A
	var list1 = Alist.Where(x => x.Data == 'A');
	// find objects with Data B
	var list2 = Alist.Where(x => x.Data == 'B');
	// find objects with Data C
	var list3 = Alist.Where(x => x.Data == 'C');

	// convert ArrayList to array of tupple
	var Aarray = A.Cast<tupple>().ToArray();

	// find objects with Data A
	var list1array = Array.FindAll(Aarray, x => x.Data == 'A');
	// find objects with Data B
	var list2array = Array.FindAll(Aarray, x => x.Data == 'B');
	// find objects with Data C
	var list3array = Array.FindAll(Aarray, x => x.Data == 'C');
}
commented: good post! +15
commented: Well done! +14

@nick.crane: I've used arraylist in all project, so maybe I can't change into another data types :|
@Mitja Bonca: Objects were added by subtract from big arraylist.

Even if your ArrayList contains many types of object if the data you are searching is of a single type (or have a common interface) then you can use the OfType method to get a strongly typed array or list.

Also, the search method can be either a named method, an anonymous method or a lambda expression. See below.

private char testData; // global variable used with tupple test method

// named method to return true when tupple Data property has value testData.
private bool FindTupple(tupple x) { return x.Data == testData; }

private void button5_Click(object sender, EventArgs e)
{
	var A = new ArrayList();
	A.Add(new tupple(1, 'A'));
	A.Add(new tupple(2, 'A'));
	A.Add(new tupple(3, 'B'));
	A.Add(new tupple(4, 'B'));
	A.Add(new tupple(5, 'B'));
	A.Add(new tupple(6, 'C'));

	// get objects of tupple type from ArrayList
	var Aarray = A.OfType<tupple>().ToArray();

	char data; // local variable to use in selection methods

	// find objects with Data 'A' using named method and global variable
	testData = 'A';
	var list1 = Array.FindAll(Aarray, FindTupple);
	// find objects with Data 'B' using anonymous delegate method and local variable
	data = 'B';
	var list2 = Array.FindAll(Aarray, delegate(tupple x) { return x.Data == data; });
	// find objects with Data 'C' using lambda expression and local variable
	data = 'C';
	var list3 = Array.FindAll(Aarray, x => x.Data == data);

}

Even if your ArrayList contains many types of object if the data you are searching is of a single type (or have a common interface) then you can use the OfType method to get a strongly typed array or list.

Also, the search method can be either a named method, an anonymous method or a lambda expression. See below.

private char testData; // global variable used with tupple test method

// named method to return true when tupple Data property has value testData.
private bool FindTupple(tupple x) { return x.Data == testData; }

private void button5_Click(object sender, EventArgs e)
{
	var A = new ArrayList();
	A.Add(new tupple(1, 'A'));
	A.Add(new tupple(2, 'A'));
	A.Add(new tupple(3, 'B'));
	A.Add(new tupple(4, 'B'));
	A.Add(new tupple(5, 'B'));
	A.Add(new tupple(6, 'C'));

	// get objects of tupple type from ArrayList
	var Aarray = A.OfType<tupple>().ToArray();

	char data; // local variable to use in selection methods

	// find objects with Data 'A' using named method and global variable
	testData = 'A';
	var list1 = Array.FindAll(Aarray, FindTupple);
	// find objects with Data 'B' using anonymous delegate method and local variable
	data = 'B';
	var list2 = Array.FindAll(Aarray, delegate(tupple x) { return x.Data == data; });
	// find objects with Data 'C' using lambda expression and local variable
	data = 'C';
	var list3 = Array.FindAll(Aarray, x => x.Data == data);

}

thanks for helping :D

@ Nick.crane: Very inspiring posts :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.