Problem with dataview sort dates

I’m using inline fields to create a table of meetings.

A problem that I face is that the sort order by date ascending doesn’t work unless the date note is actually created. Here’s an example with two entries. Assume that April 28 is a future date and I don’t have a note created for it, but I have a note created for March 30.

  • (when:: [[2023-04-28 Friday]]) (met:: Joe)
  • (when:: [[2023-03-30 Thursday]]) (met:: Peter)
TABLE WITHOUT ID item.when AS When, item.met as Met
FLATTEN file.lists as item WHERE file = this.file
SORT item.when ASC

The result is that 28 April appears first in the dataview query table. However, if I proceed to create a note dated 2023-04-28, the sort order in the dataview table becomes correct, that is in ascending order by date.

Is there a way to get the desired sort order without having to create a note for each date?

In the version below, only 2025-01-01 has a created file. Sorting works if the items are tasks.

Know this isn’t what you want, but perhaps it helps diagnostically or as alternative until someone comes along with a better solution.

Think the issue is down to regular Dataview queries querying and collating single data sets from multiple notes rather than querying multiple data sets in a single note.


- [ ] (when:: [[2025-01-01]]) (met:: Amna)
- [ ] (when:: [[2024-04-28 Friday]]) (met:: Joe)
- [ ] (when:: [[2027-01-09]]) (met:: Mitta)
- [ ] (when:: [[2024-03-30 Thursday]]) (met:: Peter)

# Sort ASC
```dataview
TASK
WHERE file = this.file
SORT outlinks ASC
LIMIT 10
```

# Sort DESC
```dataview
TASK
WHERE file = this.file
SORT outlinks DESC
LIMIT 10
```


1 Like

Topic

Summary
  • How to sort the data by the filename of a link?

Test

Summary
  • dataview: v0.5.55

Input

Summary

dictionary files

  • Location: “100_Project/02_dataview/Q73_When/Q73_test_data”

folder: Meeting

  • filename : dic_19520301
```md
---
date: 1952-03-01
---

- (when:: [[2025-01-01]]) (met:: Amna)
- (when:: [[2024-04-28 Friday]]) (met:: Joe)
- (when:: [[2027-01-09]]) (met:: Mitta)
- (when:: [[2023-03-30 Thursday]]) (met:: Peter)

### Q73_DQL10



```

folder: When

  • filename : 2023-03-30 Thursday
```md

```

DQL10_filter_by_filename_of_a_link_and_TABLE

Summary

Main DQL

Code Name Data type Group By Purposes Remark
DQL10
_filter_by_filename
_of_a_link
_and_TABLE
L.when:
a link
no 1.To split up a list file.lists into each individual element L
2.To define a field variable s_filename_of_link
3.To sort by s_filename_of_link in ascending order
4.To display the result as a table
NOTE:
The Regular Expression in the DQL10 is based on the DQL10 in the following topic.
- Solutions: by Justdoitcc

Code DQL10_filter_by_filename_of_a_link_and_TABLE

Summary_code
title: DQL10_filter_by_filename_of_a_link_and_TABLE =>1.To split up a list `file.lists` into each individual element `L` 2.To define a field variable `s_filename_of_link` 3.To sort by `s_filename_of_link` in ascending order 4.To display the result as a table
collapse: close
icon: 
color: 
```dataview
TABLE WITHOUT ID 
      L.when AS "When",
      L.met AS "Met",
      s_filename_of_link AS "s_filename_of_link"
      meta(L.when).path AS "path"

WHERE file = this.file
FLATTEN file.lists as L
FLATTEN regexreplace(meta(L.when).path, "^(.*/)(.+)(\.md)$", "$2") AS s_filename_of_link
SORT s_filename_of_link ASC
```

Screenshots(DQL10)

Part 1/2 :

Part 2/2 :


Reference

Summary

Q88_Links:Regular Expressions

```dataview

FLATTEN regexreplace(meta(up).path, "^(.*/)(.+)(\.md)$", "$2") AS s_filename_of_link 

```

2 Likes

The other posts have touched in on how to solve this, but haven’t address the issue at hand, and that is that an existing link and a non-existing link are different.

Let’s give an example:


Exisiting: [[2023-02-04]]
Non-existing: [[2025-04-02]]

```dataview
TABLE WITHOUT ID link, meta(link)
FLATTEN file.outlinks as link
WHERE file.path = this.file.path
```

In my test vault this displays as:

Do you see how the first row displays the full path of the file, whilst the second only displays the to-be-created name of the file in the path part? This affect when you sort on the links in your queries, making the order appear somewhat random depending on folders and what not.

One way to counter this is to use regex’s to remove the folder part, or a slightly simpler variant (and possibly inaccurate option in some cases) is to use the display part of the link to sort on. The latter would be erroneous if you choose to use aliases somewhere.

Still, using the latter option you could do something like:

```dataview
TABLE WITHOUT ID item.when AS When, item.met as Met
FLATTEN file.lists as item WHERE file = this.file
SORT meta(item.when).display ASC
```

And it should provide you with a sorted list.

2 Likes

Thank you for all the suggestions and guidance. The more I try and use dataview the more I understand how much power is hidden in it. Of course, sadly, I also realize that the learning curve is very, very steep for me.

1 Like

Hello.

If I run the query as suggested, Obsidian crashes.

If I limit it by adding a FROM restriction, it won’t sort.

What am I doing wrong?

- (when:: [[2025-01-01-Saturday]]) (met:: Amna)
- (when:: [[2024-04-28 Friday]]) (met:: Joe)
- (when:: [[2027-01-09-Wednesday]]) (met:: Mitta)
- (when:: [[2024-03-30 Thursday]]) (met:: Peter)

# 1 ASC
```dataview
TABLE WITHOUT ID 
item.when AS When, 
item.met AS Met
FROM "_inbox"
FLATTEN file.lists as item 
WHERE file = this.file
SORT meta(item.when).display ASC
```

# 2 DESC
```dataview
TABLE WITHOUT ID 
item.when AS When, 
item.met AS Met
FROM "_inbox"
FLATTEN file.lists as item 
WHERE file = this.file
SORT meta(item.when).display DESC
```

There is something strange happening in the background here. Try the following query (on your test data), @anon63144152:

```dataview
TABLE WITHOUT ID  
 item.when, item.met, meta(link(item.when)), map(item.outlinks, (o) => meta(o))

FLATTEN file.lists as item 
WHERE file = this.file
SORT meta(item.when).display ASC
```

For me this gives the following output:

And the strangeness is that if you do meta(item.when) it doesn’t have anything for the display part, but when you access it through the item.outlinks it does have the display part.

So in your original query you’re trying to sort on the -, null, value, so it’s left unsorted. When I used the outlink variant in my query (based on the OP’s original query), it does sort since it then has a display part.

It doesn’t crash in my setup, so I’m not sure why that’s happening. Could it be because there is some other definition of when somewhere in your test data? (It still shouldn’t crash Obsidian, though) You could try doing a query where you just list the when or item.when part, and see if you get some extra lines which somehow could case the crash when used in combination with meta().

1 Like

Many thanks.

If I copy the new query to my regular vault, Obsidian freezes, turns plain grey after about a minute, and then has to be force-quit and restarted. When I restart Obsidian, the query doesn’t appear in the file as it hasn’t been saved.

If I create a completely new vault, all of the queries run, but none of them sort correctly.

In my _inbox (in my main vault) I have a file called notes which has got the sample data and query in it. I have a second file called test that is completely empty. There are no other files in the folder. If I run the simple query below in notes, I see an extra dash in the results. If I delete the test file, the dash disappears

```dataview
TABLE WITHOUT ID
when, met
FROM "_inbox"
SORT when ASC
```

I thought that if I restricted the query to the _inbox folder that Dataview would only have a tiny set of data to look at and have no possible reason to crash. Confused.

Thanks for any ideas and suggestions, but please don’t waste time on this or me. I don’t have a need to use this specific query (at the moment) and this is all just for my own curiosity.

Takk. God helg.

Hi @anon63144152 , I’m not sure why your Obsidian crashes in some cases, as mine haven’t done so related to this stuff.

However, regarding the non-sorting situation in your last case there, do you see the number at the top of your query with the dash? It indicates that your table has only two rows, and that in the second row all four when’s are listed in one row. So in fact, the main table is sorted, but those four entries within a row isn’t sorted. (This is where one would need to do FLATTEN to go to each item separately, and sort accordingly)

Regarding the “1 asc”, “2 desc”, “3”, and “4” query and no sorting, you’re using item.when to sort on. The main point of one of my previous posts, is that you need to do the item.outlinks variant to get the .display field populated for correct sorting. If you insist on using the item.when, then you also need to the regex trickery to get to the actual date part of the links.

1 Like

Many thanks. I finally understand what’s needed and can get the queries to work.

I wiped Obsidian from my Mac over the weekend and reinstalled it from scratch. The crashes seem to have been cured.

Data and queries for anyone who wants them:

- (when:: [[2025-01-01-Saturday]]) (met:: Amna)
- (when:: [[2024-04-28 Friday]]) (met:: Joe)
- (when:: [[2027-01-09-Wednesday]]) (met:: Mitta)
- (when:: [[2024-03-30 Thursday]]) (met:: Peter)

# 1 ASC
```dataview
TABLE WITHOUT ID 
item.when AS When, 
item.met AS Met
FROM "_inbox"
FLATTEN file.lists as item 
WHERE file = this.file
SORT item.outlinks ASC
```

# 2 DESC
```dataview
TABLE WITHOUT ID 
item.when AS When, 
item.met AS Met
FROM "_inbox"
FLATTEN file.lists as item 
WHERE file = this.file
SORT item.outlinks DESC
```

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