filmov
tv
Sorting of an array in java | Sorting array in java using comparator

Показать описание
Do you want to explore how can we sort an array elements in the Java? You’re at the at right place. Please watch this video till end we’re first going to sort the array of strings then we’ll see see how we can sort the array of objects. So please sit tight & lets get started
Let us first create class with main method. Create array of string containing names. Now hit the sort method of the Arrays class. We need to import the Arrays class using import statement.
If you want to sort the array of string in descending order then we have to use the reverseOrder() method of collections class. Sorting of the such array having string is pretty simple but if you want to sort the array of objects then we’ve to use the Comparator interface.
Let’s first create the class with name Person. We’re going to add the two fields name and age. Let me generate the gets and sets along with toString() method. Create array of person objects with name Persons. Now we want to sort the Persons array by name then by age.
For this we’ve to create the another class by implementing the Comparator interface. Comparator interface has one method called compare() method. We’ve to override this method and put the logic for the sorting.
Java doc of Arrays class can be found using following link:
Java doc of Collections class can be found using following link:
Java doc of the Comparator Interface can be found using following link:
Link to download the Java:
If you like this video, please checkout other such videos:
Let us first create class with main method. Create array of string containing names. Now hit the sort method of the Arrays class. We need to import the Arrays class using import statement.
If you want to sort the array of string in descending order then we have to use the reverseOrder() method of collections class. Sorting of the such array having string is pretty simple but if you want to sort the array of objects then we’ve to use the Comparator interface.
Let’s first create the class with name Person. We’re going to add the two fields name and age. Let me generate the gets and sets along with toString() method. Create array of person objects with name Persons. Now we want to sort the Persons array by name then by age.
For this we’ve to create the another class by implementing the Comparator interface. Comparator interface has one method called compare() method. We’ve to override this method and put the logic for the sorting.
Java doc of Arrays class can be found using following link:
Java doc of Collections class can be found using following link:
Java doc of the Comparator Interface can be found using following link:
Link to download the Java:
If you like this video, please checkout other such videos:
Комментарии