@@ -107,11 +107,17 @@ public function testCustomSorting()
107
107
108
108
$ iterator = new DefaultIterator ($ this ->getLdap (), $ search );
109
109
$ sortFunction = function ($ a , $ b ) use ($ lSorted ) {
110
- if (array_search ($ a , $ lSorted ) % 2 === 0 ) {
110
+ // Sort values by the number of "1" in their binary representation
111
+ // and when that is equals by their position in the alphabet.
112
+ $ f = strlen (str_replace ('0 ' , '' , decbin (bin2hex ($ a )))) -
113
+ strlen (str_replace ('0 ' , '' , decbin (bin2hex ($ b ))));
114
+ if ($ f < 0 ) {
111
115
return -1 ;
116
+ } elseif ($ f > 0 ) {
117
+ return 1 ;
112
118
}
119
+ return strnatcasecmp ($ a , $ b );
113
120
114
- return 1 ;
115
121
};
116
122
$ iterator ->setSortFunction ($ sortFunction );
117
123
@@ -125,20 +131,20 @@ public function testCustomSorting()
125
131
126
132
$ this ->assertAttributeEquals ([
127
133
[
128
- 'resource ' => $ reflectionEntries [4 ]['resource ' ],
129
- 'sortValue ' => 'a ' ,
134
+ 'resource ' => $ reflectionEntries [1 ]['resource ' ],
135
+ 'sortValue ' => 'd ' ,
130
136
], [
131
137
'resource ' => $ reflectionEntries [0 ]['resource ' ],
132
138
'sortValue ' => 'e ' ,
133
139
], [
134
- 'resource ' => $ reflectionEntries [2 ]['resource ' ],
135
- 'sortValue ' => 'c ' ,
140
+ 'resource ' => $ reflectionEntries [4 ]['resource ' ],
141
+ 'sortValue ' => 'a ' ,
136
142
], [
137
143
'resource ' => $ reflectionEntries [3 ]['resource ' ],
138
144
'sortValue ' => 'b ' ,
139
145
], [
140
- 'resource ' => $ reflectionEntries [1 ]['resource ' ],
141
- 'sortValue ' => 'd ' ,
146
+ 'resource ' => $ reflectionEntries [2 ]['resource ' ],
147
+ 'sortValue ' => 'c ' ,
142
148
],
143
149
], 'entries ' , $ iterator );
144
150
}
0 commit comments