Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Group by - Custom function

Group by - Custom function

Scheduled Pinned Locked Moved JavaScript
javajavascripttoolsjsonhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    I have following row data in a json file: {rows:[ {id:1,data:["1","A","10"]} ,{id:2,data:["2","B","50"]} ,{id:3,data:["3","C","60"]} ,{id:4,data:["4","D","70"]} ,{id:5,data:["5","E","90"]} ,{id:6,data:["6","F","2"]} ,{id:7,data:["7","G","4"]} ,{id:8,data:["8","H","50"]} ,{id:9,data:["9","B","100"]} ,{id:10,data:["10","A","60"]} ,{id:11,data:["11","K","0"]} ,{id:12,data:["12","L","20"]} ]} I was wondering if I can get a kick start suggestion to write our own group by function to group on any column. Like in above example, we can use the second column to group by. The only restriction I have is that I can't use jquery and have to use java script only. Need expert help...

    N 1 Reply Last reply
    0
    • N NJdotnetdev

      I have following row data in a json file: {rows:[ {id:1,data:["1","A","10"]} ,{id:2,data:["2","B","50"]} ,{id:3,data:["3","C","60"]} ,{id:4,data:["4","D","70"]} ,{id:5,data:["5","E","90"]} ,{id:6,data:["6","F","2"]} ,{id:7,data:["7","G","4"]} ,{id:8,data:["8","H","50"]} ,{id:9,data:["9","B","100"]} ,{id:10,data:["10","A","60"]} ,{id:11,data:["11","K","0"]} ,{id:12,data:["12","L","20"]} ]} I was wondering if I can get a kick start suggestion to write our own group by function to group on any column. Like in above example, we can use the second column to group by. The only restriction I have is that I can't use jquery and have to use java script only. Need expert help...

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      You can overload the standard Array.sort() in the same manner as a standard comparative operation. The problem that you have is that your rows are not properly formatted for JSON (it looks like your data value should be an object, not an array) and you're mixing strings and integers. If you ONLY want to group by 1 field that's easy, if you want flexibility then you'll have a more complicated operation on your hands. One sort function will not be suitable for your needs, since string comparisons in Javascript will give you wonky results when used on stringified numbers, and using numerical comparisons on strings will also give bad data. Array.prototype.sort() - JavaScript | MDN[^] has a bunch of suggestions for how to handle sorting of various data types and uses vanilla Javascript.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups