Submit your appAboutContact

Swift data structure: how to create, sort, filter array of objects in swift

In this tutorial you'll see how to creare in swift complex data structure of array and objects and how to properly manage them. You will see how to define a data structure, how to add new items, how to filter the items, remove them or sort them. In this example we'll use a simple list of objects that contains random generated numbers.

How to define a custom object in swift

In order to define a custom object in swift, you'll need to define a struct data format, by defining it with the name of the new object you're creating and with the custom data structure. Moreover, you can use the init function to create a struct constructor. Let's define the data structure that we'll use in this example. It will be called numbers, and contains two fields: name and number. We'll use also the constructor to directly initilize the data.

struct numbers
	var name:String = ""
	var number:Int = 0

	init(name:String, number:Int)
	{ = name
		self.number = number

How to define an array of custom objects in swift

Our custom object is now defined, we'll need now to create a list of this kind of object. By using the brackets we'll tell swift that a variable has to be intended as an array. We'll define in this way the array that contains the numbers data structure and we'll initialize it. In this way this variable is ready to be used.

var list:[numbers] = [numbers]()

Let's fill the array with random number

We'll use the arc4random_uniform swift function to generate random numbers. In this example 100 random numbers have been generated and appended to the list as numbers objects. We'll use the object constructor that we'll previously defined to directly fill the data inside the object. Using the append function, we can directly append new elements to the list.

for (var i = 0; i < 100; i++)
	let random_number:Int = Int(arc4random_uniform(100))
	list.append(numbers(name: random_number.description, number: random_number))

Use the filter swift function to filter custom object properties

We'll now want to create a new list, with numbers that are lower than 10. Using the filter function we'll able to isolate the objects that satisfy this condition and achieve this kind of requirment.

var filtered_list = list.filter({$0.number < 10})

The filter swift function allows us to define which field of our custom object to filter, number in this case.

Use the sort function to order the list on the basis of a custom object properties

We'll now need to order the list, by putting the number in an ascending order. By using the sort function we'll able to do that.

var sorted = list.sort({$0.number < $1.number})

How to remove elements from the array

We'll now want to remove some elements from the array. This can be achieved by using the removeAtIndex swift utility.


Where 0 is the index of the array that you want to remove. If you want to search for a particular index of the array to remove, then you'll need to use the IndexOf function. By using this function you'll able to define a search condition and the indexOf function will return the index to pass to the removeAtIndex function.


Leave a comment

Submit your appAboutContactPrivacy