Skip to content

Commit 65d2427

Browse files
committed
Sort objects alphabetically #4 (Add option to sort statements alphabetically)
1 parent 454cf5e commit 65d2427

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

src/lib/Body.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Alert, Box, Divider } from '@mui/material'
22
import React, { useMemo } from 'react'
33
import { useViewerContext } from './viewer-context'
44
import Predicate from './Predicate'
5-
import { formatIRI } from './format'
5+
import { comparePredicates } from './common'
66

77
function Body (): JSX.Element {
88
const viewerCtx = useViewerContext()
@@ -13,12 +13,7 @@ function Body (): JSX.Element {
1313
const entries = useMemo(() => {
1414
if (predicates === undefined) return undefined
1515
return Object.entries(predicates)
16-
// Sort predicates alphabetically
17-
.sort(([a], [b]) => {
18-
const fmtA = formatIRI(viewerCtx, a)
19-
const fmtB = formatIRI(viewerCtx, b)
20-
return fmtA.localeCompare(fmtB)
21-
})
16+
.sort(([a], [b]) => comparePredicates(viewerCtx, a, b))
2217
}, [predicates])
2318

2419
if (entries === undefined) {

src/lib/Objects.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Objects as ObjectsI, Object as ObjectI } from './rdf-json'
22
import React, { useMemo } from 'react'
3-
import { stringToHashNumber } from './hash'
43
import { useViewerContext } from './viewer-context'
54
import Qualifiers from './Qualifiers'
65
import SimpleObject from './SimpleObject'
76
import { usePredicateContext } from './predicate-context'
87
import SeeMoreButton from './SeeMoreButton'
9-
import { isStandardReifiedStatement } from './common'
8+
import { compareObjects, isStandardReifiedStatement } from './common'
109

1110
interface Props {
1211
objects: ObjectsI
@@ -15,14 +14,12 @@ interface Props {
1514
function Objects (props: Props): JSX.Element {
1615
const { objects } = props
1716

18-
const { data, labelIRIs } = useViewerContext()
17+
const viewerCtx = useViewerContext()
18+
const { data, labelIRIs } = viewerCtx
1919
if (data === undefined) throw new Error('Objects: data is undefined')
2020
const { howManyVisibleObjects, setHowManyVisibleObjects } = usePredicateContext()
2121

22-
// Values sorted by type
23-
const sortedObjects = useMemo(() => objects.sort((a, b) => (
24-
stringToHashNumber(a.type) - stringToHashNumber(b.type)
25-
)), [objects])
22+
const sortedObjects = useMemo(() => objects.sort((a, b) => compareObjects(viewerCtx, a, b)), [objects])
2623

2724
// Make groups of objects based on whether they are bnodes or not
2825
const groups = useMemo(() => {

src/lib/Qualifiers.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Box, TableCell, TableRow } from '@mui/material'
2+
import { useMemo } from 'react'
3+
import { compareObjects, comparePredicates } from './common'
24
import { CopyIRIButton } from './CopyButton'
35
import { formatIRI, getLabel } from './format'
46
import QualifierContainer from './QualifierContainer'
@@ -20,7 +22,10 @@ function Qualifiers (props: Props): JSX.Element | null {
2022
const predicates: Predicates | undefined = data[qualifierObject.value]
2123
if (predicates === undefined) return null
2224

23-
const predicateKeys = Object.keys(predicates)
25+
const predicateKeys = useMemo(() => {
26+
return Object.keys(predicates)
27+
.sort((a, b) => comparePredicates(viewerCtx, a, b))
28+
}, [predicates])
2429

2530
return (
2631
<QualifierContainer>
@@ -33,7 +38,7 @@ function Qualifiers (props: Props): JSX.Element | null {
3338
</LinkComponent>
3439
</TableCell>
3540
<TableCell>
36-
{predicates[predicate].map((object, i) => (
41+
{predicates[predicate].sort((a, b) => compareObjects(viewerCtx, a, b)).map((object, i) => (
3742
<Box key={i}>
3843
<SimpleObject object={object} noContainer />
3944
</Box>

src/lib/common.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { formatIRI } from './format'
2+
import { stringToHashNumber } from './hash'
13
import { Object as ObjectI, RdfJson } from './rdf-json'
4+
import { ViewerContextI } from './viewer-context'
25

36
export function isStandardReifiedStatement (data: RdfJson, object: ObjectI, labelIRIs: string[]): boolean {
47
// After having removed the label predicates/objects, we check if the statement's object is
@@ -15,3 +18,17 @@ export function isStandardReifiedStatement (data: RdfJson, object: ObjectI, labe
1518
.filter(([p]) => !labelIRIs.includes(p)) // Remove label predicates
1619
.length > 0 // Check if there are any predicates left
1720
}
21+
22+
export function comparePredicates (viewerCtx: ViewerContextI, a: string, b: string): number {
23+
const fmtA = formatIRI(viewerCtx, a)
24+
const fmtB = formatIRI(viewerCtx, b)
25+
return fmtA.localeCompare(fmtB)
26+
}
27+
28+
export function compareObjects (viewerCtx: ViewerContextI, a: ObjectI, b: ObjectI): number {
29+
const deltaType = stringToHashNumber(a.type) - stringToHashNumber(b.type)
30+
if (deltaType !== 0) return deltaType
31+
const fmtA = formatIRI(viewerCtx, a.value)
32+
const fmtB = formatIRI(viewerCtx, b.value)
33+
return fmtA.localeCompare(fmtB)
34+
}

0 commit comments

Comments
 (0)