Quantcast
Channel: Why is my implementation of array.sorted( by: ) in Swift returning a strange order? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Why is my implementation of array.sorted( by: ) in Swift returning a strange order?

$
0
0

I have a custom class:

class MyClass: MySuperClass{    var myNumber: Int    required init ( id: Int, myNumber: Int )    {        self.myNumber = myNumber        super.init( id: id )    }    required init ( id: Int )    {         fatalError( "init() has not been implemented" )     }}

I create an array of these objects of length 20:

var myNumbers: [ MyClass ] = []

I iterate through the array, printing out the values of myNumber:

0112210018394000001222601100024712

I then run a sort on them:

myNumbers.sorted( by: { $0.myNumber < $1.myNumber } )

And I iterate through the array a second time, printing out the values of myNumber:

0000000000021624711101222122118394

As you can see it sort of sorts them, but not very satisfactorily.

What am I doing wrong please? Thank you in advance.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images