Created
May 19, 2023 16:25
-
-
Save dorman99/8ffabb26686dc301f165c8cc2c7d313b to your computer and use it in GitHub Desktop.
Pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
// var listInts []int = []int{9, 4, 5, 6, 7} | |
var Pages = [][]string{ | |
{"p1", "10", "300"}, | |
{"p2", "13", "500"}, | |
{"p3", "52", "899"}, | |
{"p4", "10", "500"}, | |
} | |
var OtherPages = [][]string{ | |
{"buku55", "4819401", "999999"}, | |
{"buku1", "4819401", "359199"}, | |
{"buku2", "8571831", "994193"}, | |
{"buku3", "4819401", "495104"}, | |
{"buku4", "5018414", "559141"}, | |
{"buku5", "5919412", "509149"}, | |
} | |
func main() { | |
orderBy := 2 // name, relevance, price | |
orderType := 1 // asc || desc | |
pageNumber := 0 | |
totalDataPerPage := 2 | |
fmt.Println("Input", OtherPages) | |
sp1 := SortPage(OtherPages, orderBy, orderType) | |
test1 := ReturnPage(pageNumber, totalDataPerPage, sp1) | |
fmt.Println("Sorted 1", sp1) | |
fmt.Println("test 1: ", test1) | |
fmt.Println("-------") | |
orderBy = 0 // name, relevance, price | |
orderType = 1 // asc || desc | |
pageNumber = 1 | |
totalDataPerPage = 3 | |
fmt.Println("Input", Pages) | |
sp2 := SortPage(Pages, orderBy, orderType) | |
test2 := ReturnPage(pageNumber, totalDataPerPage, sp2) | |
fmt.Println("Sorted 2", sp2) | |
fmt.Println("test 2: ", test2) | |
} | |
func ReturnPage(pageNumber, totalDataPerPage int, page [][]string) []string { | |
listedPage := [][]string{} | |
dataPerPage := []string{} | |
for i := 0; i <= len(page)-1; i++ { | |
dataPerPage = append(dataPerPage, page[i][0]) | |
if len(dataPerPage) == totalDataPerPage { | |
listedPage = append(listedPage, dataPerPage) | |
dataPerPage = []string{} | |
} | |
} | |
if len(dataPerPage) > 0 { | |
listedPage = append(listedPage, dataPerPage) | |
} | |
return listedPage[pageNumber] | |
} | |
func SortPage(arr [][]string, ob int, ot int) [][]string { | |
sort.Slice(arr, func(i, j int) bool { | |
if ot == 0 { | |
return arr[i][ob] < arr[j][ob] | |
} | |
return arr[i][ob] > arr[j][ob] | |
}) | |
return arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment