简体   繁体   中英

How to sort an object in JavaSript

I am trying to sort the variable b alphabetically but for some reason, I am getting an error, obj sort is undefined . This code is written on a visual basic form with tags to write js. the return of the variable b is: Extra,Admin1,Alfa,Manda,Alfa2,LCDDF

function JSFORM_Start() {
  var b =JSDLS_GetStr("USERS")
  alert(b);
  var a= b.split(",");
  alert(a);
  a= a.sort(",");
  alert(a);
  var c=a.join(",")
  ss("My users.choices", c)
}

Any idea how to do this correctly. Thanks.

I think you have a typo in your code at a = a.sort(",");

As you can see here , the sort() function either takes no parameters at all, or one parameter - a comparison function.

you can do it same as follow:

 let a=[9,5,7,2,1]; a = a.sort(); console.log(a); console.log(a.reverse());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM