Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting notes alphabetically now sort starting numbers in titles right #2910

Merged
merged 4 commits into from
May 9, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@ const { remote } = require('electron')
const { dialog } = remote
const WP_POST_PATH = '/wp/v2/posts'

const regexMatchStartingTitleNumber = new RegExp('^([0-9]*\.?[0-9]+).*$')

function sortByCreatedAt (a, b) {
return new Date(b.createdAt) - new Date(a.createdAt)
}

function sortByAlphabetical (a, b) {
const matchA = regexMatchStartingTitleNumber.exec(a.title)
const matchB = regexMatchStartingTitleNumber.exec(b.title)

if (matchA && matchA.length === 2 && matchB && matchB.length === 2) {
// Both note titles are starting with a float. We will compare it now.
const floatA = parseFloat(matchA[1])
const floatB = parseFloat(matchB[1])

if (Math.abs(floatA - floatB) > 0.01) {
dredav marked this conversation as resolved.
Show resolved Hide resolved
return floatA - floatB
}

// The float values are equal. We will compare the full title.
}

return a.title.localeCompare(b.title)
}

Expand Down