diff --git a/src/components/ValidatorList/ValidatorTable/ValidatorTable.js b/src/components/ValidatorList/ValidatorTable/ValidatorTable.js index 2ee76d19..170cc79d 100644 --- a/src/components/ValidatorList/ValidatorTable/ValidatorTable.js +++ b/src/components/ValidatorList/ValidatorTable/ValidatorTable.js @@ -9,7 +9,7 @@ import { _ } from "src/lib/scripts"; import { tableThemes } from "src/constants/tableThemes"; import { sortDirections } from "src/constants/sortDirections"; import consts from "src/constants/consts"; -import { formatPercentage, formatInteger, formatOrai } from "src/helpers/helper"; +import { formatPercentage, formatInteger, formatOrai, groupAndShuffle } from "src/helpers/helper"; import { compareTwoValues } from "src/helpers/compare"; import Delegate from "src/components/common/Delegate"; import ThemedTable from "src/components/common/ThemedTable"; @@ -67,7 +67,7 @@ const toggleDirection = direction => { }; const ValidatorTable = memo(({ data = [] }) => { - const [sortField, setSortField] = useState(sortFields.UPTIME); + const [sortField, setSortField] = useState(); const [sortDirection, setSortDirection] = useState(sortDirections.DESC); const [canSort, setCanSort] = useState(false); const [isFirstSort, setIsFirstSort] = useState(true); @@ -214,19 +214,9 @@ const ValidatorTable = memo(({ data = [] }) => { const sortData = (data, extraSortField = sortFields.RANK) => { if (!data) return []; - if (isFirstSort) { - return [...data] - .map(e => { - return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime }; - }) - .sort(function(a, b) { - if (a["votingPowerMixUpTime"] === b["votingPowerMixUpTime"]) { - return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], toggleDirection(sortDirection)); - } else { - return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], sortDirection); - } - }); + const groupSize = 10; + return groupAndShuffle(data, groupSize).flat(); } if (canSort) { diff --git a/src/helpers/helper.js b/src/helpers/helper.js index f6e3e149..b6febdd0 100644 --- a/src/helpers/helper.js +++ b/src/helpers/helper.js @@ -376,3 +376,25 @@ export const calculateTallyProposal = ({ totalVote, bonded, tally }) => { vote_percentage: votePercentage, }; }; + +export function shuffleArray(array) { + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; +} + +export function groupAndShuffle(array, groupSize) { + const sortedArray = array + .map(e => { + return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime }; + }) + .sort((a, b) => b.votingPowerMixUpTime - a.votingPowerMixUpTime); + const groups = []; + for (let i = 0; i < sortedArray.length; i += groupSize) { + groups.push(sortedArray.slice(i, i + groupSize)); + } + const shuffledGroups = groups.map(group => shuffleArray(group)); + return shuffledGroups; +}