JavaScript:如何通过两个对象属性对对象数组进行排序?

时间:2016-03-17 03:28:13

标签: javascript arrays sorting

我希望通过两个对象属性对下面的对象数组进行排序。数组的排序顺序应该是最低distance属性,如果距离之间存在平局,则应按resource_name_line_1属性按字母顺序排序。

[ 
    {
        id: 23,
        resource_name_line_1: 'Steve Y',
        resource_name_line_2: null,
        resource_handle: 'd59ca9daf8',
        zip_display_name: 'Concord, MA (01742)',
        lat: 42.4606,
        long: -71.3642,
        distance: 0.0014901018498154799 
    },
    {
        id: 42,
        resource_name_line_1: 'HelloWorld123ABC',
        resource_name_line_2: 'Test',
        resource_handle: '4684ea475b',
        zip_display_name: 'Waltham, MA (02453)',
        lat: 42.37,
        long: -71.2327,
        distance: 4.4403602189547104 
    },
    {
        id: 22,
        resource_name_line_1: 'Steve X',
        resource_name_line_2: null,
        resource_handle: '899c5bfafc',
        zip_display_name: 'Concord, MA (01742)',
        lat: 42.4606,
        long: -71.3642,
        distance: 4.4403602189547104 
    },
    {
        id: 25,
        resource_name_line_1: 'Ariel Z',
        resource_name_line_2: null,
        resource_handle: 'a99dc0c01e',
        zip_display_name: 'Boston, MA (02108)',
        lat: 42.3573,
        long: -71.0645,
        distance: 7.345061746122304 
    },
    {
        id: 40,
        resource_name_line_1: 'Steve MM',
        resource_name_line_2: null,
        resource_handle: '045397198d',
        zip_display_name: 'Waltham, MA (02453)',
        lat: 42.37,
        long: -71.2327,
        distance: 4.4403602189547104 
    },
    {
        id: 1,
        resource_name_line_1: 'CVS',
        resource_name_line_2: 'Pharmacy',
        resource_handle: '821582z6d0',
        zip_display_name: 'Boston, MA (02108)',
        lat: 42.3573,
        long: -71.0645,
        distance: 7.345061746122304 
    },
    {
        id: 2,
        resource_name_line_1: 'LabCorp',
        resource_name_line_2: 'Test',
        resource_handle: '35r61ds539',
        zip_display_name: 'Boston, MA (02108)',
        lat: 42.3573,
        long: -71.0645,
        distance: 7.345061746122304 
    },
    {
        id: 3,
        resource_name_line_1: 'Fred\'s Pharmacy',
        resource_name_line_2: 'Test',
        resource_handle: '82b583d6d0',
        zip_display_name: 'Boston, MA (02123)',
        lat: 42.3586,
        long: -71.0605,
        distance: 7.567375602408066 
    } 
]

0 个答案:

没有答案