obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

dataviewjs dv.table sorting does not appear to work on data from property json

Open btmurrell opened this issue 1 year ago • 1 comments

What happened?

I keep a json object string in my property:

---
team: {"123":{"name":"jack white","team":"","notes":"",},"456":{"name":"jack brown","team":"","notes":"",},"789":{"name":"jack black","team":"","notes":"",}}

I store and object because I have not found a way to store an array. So, I convert my object to an array with Object.keys as seen below, then want to render a table with it (all code below). The table renders fine, but I cannot get the .sort() function to actually sort. I wonder if there's a problem in

  • using data from a property?
  • transforming property data from object to an array?

just to make sure i had a dv.array, i did pass my team array to dv.array(team), but that had no effect.

fwiw, just for readability, formatted version here in this issue just for reference:

team: {
  "123": {
    "name": "jack white",
    "team": "",
    "notes": "",
    
  },
  "456": {
    "name": "jack brown",
    "team": "",
    "notes": "",
    
  },
  "789": {
    "name": "jack black",
    "team": "",
    "notes": "",
    
  }
}

DQL

No response

JS

const _team = dv.current().team;
const team = Object.keys(_team).map(sso => {
  return {
    name: _team[sso].name,
    sso,
    team: _team[sso].team,
    notes: _team[sso].notes,
  }
});
dv.table(
  ['Name', 'SSO', 'Team', 'Notes'],
  team
  .map(node => [node.name, node.sso, node.team, node.notes])
  .sort(node => node.name)
);


### Dataview Version

0.5.64

### Obsidian Version

1.5.3

### OS

Windows

btmurrell avatar Feb 20 '24 01:02 btmurrell

That's not a bug, after you've mapped the values the query doesn't know what is the name part anymore.

Switch around your .map() and .sort(), and it'll most likely sort itself out. Alternatively, do a .sort(a => a[0]), which would be the new reference to make after the mapping.

holroy avatar Feb 25 '24 01:02 holroy