Smalltalk Collections

If I have an array of employees, how can I sort based on the name of the employee?

+3
source share
2 answers

It should be something like:

employees sortBy: [:a :b | a lastName > b lastName]
+6
source

If we make these assumptions:

  • An instance of the array is stored in a variable named employee
  • Array contains a set of instances that all respond to the lastName message, returning an instance of String
  • You want to sort the collection in ascending order.

Then you can complete the task with the following code snippet:

 employees asSortedCollection: [ :a :b | a lastName < b lastName ]

asSortedCollection: Array employee. Block, , . , , , a b | . | SortedCollection.

, , , , . , ( , asSortedCollection:), , .

+6

Source: https://habr.com/ru/post/1719467/


All Articles