根据键的存在和值对对象数组进行排序

时间:2016-11-04 16:31:44

标签: javascript arrays sorting

我有一个对象数组。例如 -

[{
    aKey:2, 
    bKey:2, 
    cKey:3
}, { 
    bKey:2, 
    cKey:6
}, {
    aKey:1,
    bKey:6, 
    cKey:5
}, {
    bKey:1, 
    cKey:4
}, {
    bKey:6, 
    cKey:7
}]

所以我需要做的是 -

  1. 首先根据aKey(asc顺序)对数组进行排序,具有此键的对象将位于结果数组的开头。
  2. 然后我需要根据bKey的值对数组进行排序。例如,所有bKey = 2的记录都在开头。
  3. 休息记录将根据按顺序排列的cKey值进行排序。
  4. 所以输出将是 -

    [{
        aKey:1,
        bKey:6, 
        cKey:5
    }, {
        aKey:2, 
        bKey:2, 
        cKey:3
    }, { 
        bKey:2, 
        cKey:6
    }, {
        bKey:1, 
        cKey:4
    }, {
        bKey:6, 
        cKey:7
    }]
    

2 个答案:

答案 0 :(得分:2)

您可以像这样使用protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .csrf() .disable() .exceptionHandling() .authenticationEntryPoint(authenticationEntryPoint) .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() .antMatchers( HttpMethod.GET, "/", "/*.html", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js", "/**/**/*.css", "/**/**/*.js", "/**/**/*.html", "/**/**/**/*.css", "/**/**/**/*.js", "/**/**/**/*.html", "/**/**/**/**/*.css", "/**/**/**/**/*.js" ).permitAll() .antMatchers("/auth/**", "/").permitAll() .anyRequest().authenticated(); // Custom JWT based authentication httpSecurity .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); }



sort()




答案 1 :(得分:2)

要优先从aKeybKey再到cKey进行排序,您可以使用:

var array=[{aKey:2,bKey:2,cKey:3},{bKey:2,cKey:6},{aKey:1,bKey:6,cKey:5},{bKey:1,cKey:4},{bKey:6,cKey:7}]

var result = array.sort(function(hash) {
  return function(a, b) {
    return ((a.aKey || Infinity) - (b.aKey || Infinity)) 
    || ((a.bKey || Infinity) - (b.bKey || Infinity)) 
    || ((a.cKey || Infinity) - (b.cKey || Infinity))
  }
}(Object.create(null)));

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

  

但是你希望bKey:2bKey:1之前到达最后一个元素   如果aKeybKey,则2的值为aKey

调整此异常,在bKey完成后不知道应该关注哪个元素(并扩展到var array=[{aKey:2,bKey:2,cKey:3},{aKey:1,bKey:6,cKey:5},{bKey:1,cKey:4},{bKey:6,cKey:7},{bKey:2,cKey:7},{bKey:2,cKey:6},{cKey:4},{cKey:7}] var result = array.sort(function(hash) { return function(a, b) { // find the anomaly keys a.aKey && !b.aKey && (hash.bkey = a.bKey); a.bKey && !b.bKey && (hash.ckey = a.cKey); // sort criteria return ((a.aKey || Infinity) - (b.aKey || Infinity)) || (((a.bKey != hash.bkey) - (b.bKey != hash.bkey)) || ((a.bKey || Infinity) - (b.bKey || Infinity))) || (((a.cKey != hash.ckey) - (b.cKey != hash.ckey)) || ((a.cKey || Infinity) - (b.cKey || Infinity))) } }(Object.create(null))); console.log(result);也完成的情况) ,你可以这样做 - 哈希这些异常密钥并相应地排序 - 见下面的演示:

.as-console-wrapper{top:0;max-height:100%!important;}
@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=false",
        "javax.portlet.name=cpportlet",
        "javax.portlet.display-name=Control Panel Portlet",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class CpPortlet extends GenericPortlet {

    @Override
    protected void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

        PrintWriter printWriter = renderResponse.getWriter();

        printWriter.print("cp-portlet Portlet - Hello World!");
    }

}

相关问题