I have a search results page that displays 10 results from an array of JSON objects based on what the user entered into thier search.
Currently the order in which they display is the same as the appear in the file. Is there a way to order my results with the closest match first and then second closest second and so on.
So I essentially need to order the array by the same kind of filter I used for the id.
** I understand that by using text several times it wont work as effectively **
Heres the component:
import * as React from 'react';
import { useParams } from "react-router-dom";
import { content } from "./reviews";
import { Link } from "react-router-dom";
export default function Results() {
//Extracts ID from URL
const {id} = useParams();
return (
<div className="mx-2 my-16 lg:w-[60%] lg:m-auto pt-0.5 lg:py-16 sm:px-3 bg-neutral-200">
<h3 className="bg-red-500 font-bold text-white px-4 py-0.5">Showing results for: {id}</h3>
{content
//Filter through JSON
.filter((content) => {
//If any input characters much object characters return corresponding object
if (
content.name.toLowerCase().includes(id.toLocaleLowerCase())
) {
return content;
}
})
//Maps element based on the number of JSON objects
.map((content, index) => {
return (
index < 10 && (
<Link to={content.link}>
<a href={content.link}>
<div id="thumbnail" className="flex flex-row py-4 px-3 bg-white border-b-2 border-red-300 space-x-4">
<Link to={content.link} className="w-2/5 sm:w-1/3 lg:mr-0 rounded-sm">
<img src={content.image} alt="Review thumbnail"></img>
</Link>
<div className="md:flex md:flex-col space-y-2">
<h5 className="font-bold ">{content.heading}</h5>
<p>{content.text}</p>
</div>
</div>
</a>
</Link>
)
);
})
};
</div>
);
};
Heres the JSON array:
export const content = [
{link: "/review/elden-ring", name: "Elden\nRing", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text1", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text2", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text3", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text4", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text5", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text6", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text7", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text8", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text9", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text10", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
{link: "/review/elden-ring", name: "text11", image: '../images/elden-ring-thumbnail.jpg', heading: 'text1', text: 'text1'},
];