PHP 根据对象属性进行对象数组的排序
2018-02-01 14:34
976 查看
根据对象属性进行对象数组的排序【转自 stackoverflow 】
Sort array of objects by object fields
Question
How can I sort this array of objects by one of its fields, like name or count ?
Answer :
Use
edits imported from comments:
If you’re sorting the array from inside the class and your sorting function cmp is also defined inside the class, then use this:
Sort array of objects by object fields
Question
How can I sort this array of objects by one of its fields, like name or count ?
Array ( [0] => stdClass Object ( [ID] => 1 [name] => Mary Jane [count] => 420 ) [1] => stdClass Object ( [ID] => 2 [name] => Johnny [count] => 234 ) [2] => stdClass Object ( [ID] => 3 [name] => Kathy [count] => 4354 ) ....
Answer :
Use
usort, here’s an example adapted from the manual:
function cmp($a, $b) { return strcmp($a->name, $b->name); } usort($your_data, "cmp");
edits imported from comments:
If you’re sorting the array from inside the class and your sorting function cmp is also defined inside the class, then use this:
usort($your_data, array($this, "cmp"))
参考:
https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields#相关文章推荐
- 根据对象某一属性进行排序
- js数组排序,根据数组中对象的某一属性进行排序 支持数字和字符串的排序,也支持按两个属性进行排序
- C#中对List<Object>泛型集合,根据对象的某一属性进行升序排序
- 根据对象的某一属性进行排序的js代码(如:name,age)
- 如何根据对象的属性,对集合(list / set)中的对象进行排序
- js根据对象的某一属性进行排序
- Java实现根据List中对象的相应属性进行排序
- 根据对象的某一属性进行排序的js代码(如:name,age)
- JavaScript 对象数组,根据某个对象属性进行排序
- List集合中删除属性(一个或者多个属性)相同的对象返回List且根据对象属性进行排序输出
- 对象集合根据时间属性进行排序
- 根据list对象的某一个属性进行排序
- js,jquery 根据对象某一属性进行排序
- 根据某一属性对数组中的对象进行排序
- 根据List的对象的某个属性进行排序方法
- 对象数组根据单个属性进行排序
- List根据里面的对象属性的值进行排序
- 如何根据对象的属性,对集合(list / set)中的对象进行排序
- java编程如何将对象数组按对象中的某一个属性进行排序
- 【原创】Java 对 List 内对象根据对象属性进行排序