Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 46123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:55:00+00:00 2026-05-10T15:55:00+00:00

Can someome provide code or pseudo-code for how the paging links on StackOverflow are

  • 0

Can someome provide code or pseudo-code for how the paging links on StackOverflow are generated?

I keep racking my brain but can’t think of a decent way to build the dynamic links that always show the 2 pages around the current, plus the first and last.

Example: 1 ... 5 6 7 ... 593

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:55:01+00:00Added an answer on May 10, 2026 at 3:55 pm

    There are several other answers already, but I’d like to show you the approach I took to solve it: First, let’s check out how Stack Overflow handles normal cases and edge cases. Each of my pages displays 10 results, so to find out what it does for 1 page, find a tag that has less than 11 entries: usability works today. We can see nothing is displayed, which makes sense.

    How about 2 pages? Find a tag that has between 11 and 20 entries (emacs works today). We see: ‘1 2 Next’ or ‘Prev 1 2‘, depending on which page we’re on.

    3 pages? ‘1 2 3 … 3 Next’, ‘Prev 1 2 3 Next’, and ‘Prev 1 … 2 3‘. Interestingly, we can see that Stack Overflow itself doesn’t handle this edge case very well: it should display ‘1 2 … 3 Next’

    4 pages? ‘1 2 3 … 4 Next’, ‘Prev 1 2 3 … 4 Next’, ‘Prev 1 … 2 3 4 Next’ and ‘Prev 1 … 3 4‘

    Finally let’s look at the general case, N pages: ‘1 2 3 … N Next’, ‘Prev 1 2 3 … N Next’, ‘Prev 1 … 2 3 4 … N Next’, ‘Prev 1 … 3 4 5 … N Next’, etc.

    Let’s generalize based on what we’ve seen: The algorithm seems to have these traits in common:

    • If we’re not on the first page, display link to Prev
    • Always display the first page number
    • Always display the current page number
    • Always display the page before this page, and the page after this page.
    • Always display the last page number
    • If we’re not on the last page, display link to Next

    Let’s ignore the edge case of a single page and make a good first attempt at the algorithm: (As has been mentioned, the code to actually print out the links would be more complicated. Imagine each place we place a page number, Prev or Next as a function call that will return the correct URL.)

    function printPageLinksFirstTry(num totalPages, num currentPage)   if ( currentPage > 1 )     print 'Prev'   print '1'   print '...'   print currentPage - 1   print currentPage   print currentPage + 1   print '...'   print totalPages   if ( currentPage < totalPages )     print 'Next' endFunction 

    This function works ok, but it doesn’t take into account whether we’re near the first or last page. Looking at the above examples, we only want to display the … if the current page is two or more away.

    function printPageLinksHandleCloseToEnds(num totalPages, num currentPage)   if ( currentPage > 1 )     print 'Prev'   print '1'   if ( currentPage > 2 )     print '...'   if ( currentPage > 2 )     print currentPage - 1   print currentPage   if ( currentPage < totalPages - 1 )     print currentPage + 1   if ( currentPage < totalPages - 1 )     print '...'   print totalPages   if ( currentPage < totalPages )     print 'Next' endFunction 

    As you can see, we have some duplication here. We can go ahead and clean that up for readibility:

    function printPageLinksCleanedUp(num totalPages, num currentPage)   if ( currentPage > 1 )     print 'Prev'   print '1'   if ( currentPage > 2 )     print '...'     print currentPage - 1   print currentPage   if ( currentPage < totalPages - 1 )     print currentPage + 1     print '...'   print totalPages   if ( currentPage < totalPages )     print 'Next' endFunction 

    There are only two problems left. First, we don’t print out correctly for one page, and secondly, we’ll print out ‘1’ twice if we’re on the first or last page. Let’s clean those both up in one go:

    function printPageLinksFinal(num totalPages, num currentPage)   if ( totalPages == 1 )     return    if ( currentPage > 1 )     print 'Prev'    print '1'    if ( currentPage > 2 )     print '...'     print currentPage - 1    if ( currentPage != 1 and currentPage != totalPages )     print currentPage    if ( currentPage < totalPages - 1 )     print currentPage + 1     print '...'    print totalPages    if ( currentPage < totalPages )     print 'Next'  endFunction 

    Actually, I lied: We have one remaining issue. When you have at least 4 pages and are on the first or last page, you get an extra page in your display. Instead of ‘1 2 … 10 Next’ you get ‘1 2 3 … 10 Next’. To match what’s going on at Stack Overflow exactly, you’ll have to check for this situation:

    function printPageLinksFinalReally(num totalPages, num currentPage)   if ( totalPages == 1 )     return    if ( currentPage > 1 )     print 'Prev'    print '1'    if ( currentPage > 2 )     print '...'     if ( currentPage == totalPages and totalPages > 3 )       print currentPage - 2     print currentPage - 1    if ( currentPage != 1 and currentPage != totalPages )     print currentPage    if ( currentPage < totalPages - 1 )     print currentPage + 1     if ( currentPage == 1 and totalPages > 3 )       print currentPage + 2     print '...'    print totalPages    if ( currentPage < totalPages )     print 'Next'  endFunction 

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Found it! It's nasty, though, since it involved editing the… May 11, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer Apparently this should not have been sop difficult. Even though… May 11, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer Do this....Drop the article. Create a new storedprocedure in the… May 11, 2026 at 9:18 pm

Related Questions

I'm creating some big files (DB exports) with Java and I need to put
Our app (already deployed) is using an Access/Jet database. The upcoming version of our
My first instinct is to say each key is an object, and has a
I keep seeing the phrase duck typing bandied about, and even ran across a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.