DataviewJS sort DataArray numerically

Given a list of files which all have titles in the form

Booktitle Chapternumber.md

(for example ‘Book 1.md’, ‘Book 2.md’)
I want to order those file names by their chapter number.
If I have chapters 1,2,10,3 - it will order as

  • 1
  • 10
  • 2
  • 3

because internally JS is using a lexicographic order.
What I want is a numeric order as

  • 1
  • 2
  • 3
  • 10

My current attempt was

dv.pages('"folder"').file.where(f => f.name.contains('Book').sort(f => f.name.replace('Book ','')[0], 'asc')

This however uses the unwanted order type; how to get the numbers right guys? :wink:

Why do you do the [0], is your return an array?

You for starters try adding number() around your expression in the sort function. If that doesn’t work, I would list out the value you’re trying to sort on to verify it’s what you think it is.

number() is not defined according to error message.
parseInt() also has no effect.
I removed the square brackets, same result.
I think it’s not the data input, it’s the sorting algorithm that needs to be changed.

Sorry, my bad for recommending the number(), that is the DQL variant to convert to numbers. parseInt() should on the other hand (or the sneaky way of dv.func.number().

As you’ve established the default sorting algorithm seems to be the pure alphanumeric order, and getting dataview to shift to natural sort order could cause some controversy, so I do believe changing the sorting input to be numeric is the way forward.

And as hinted at before I think the next step is to verify what value your actually sorting on. So I’d try adding the sorting value to your column output, with or without an attempt to convert into numbers. Report back and we can take this further discussing options.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.