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

  • SEARCH
  • Home
  • 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 8457193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:44:54+00:00 2026-06-10T12:44:54+00:00

I have a couchdb that contains events with their starting time and their coordinates.

  • 0

I have a couchdb that contains events with their starting time and their coordinates. I wrote a list view that calculates the distance from your current location to those events like follows:

locateEvents: function(head, req){ 

        var row, comma = ''; 
        start({
            "headers": {
                "Content-Type": "application/json"
            }
        });
        if(req.query.latitude&&req.query.longitude&&req.query.radius&&req.query.now){

            var R = 6371; // km
            var dLon, dLat, lat1, lat2;

            var results = [];
            while(row = getRow()) { 
                dLon = Math.abs(row.value.venue.longitude-req.query.longitude);
                dLat = Math.abs(row.value.venue.latitude-req.query.latitude);

                dLon = (dLon*3.14159)/180;
                dLat = (dLat*3.14159)/180;

                lat1 = (Math.abs(req.query.longitude)*3.14159)/180;
                lat2 = (Math.abs(req.query.latitude)*3.14159)/180;

                var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
                var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
                var d = R * c;

                if((d < req.query.radius)&&(row.value.start_time > req.query.now)){
                    results.push(row.value);
                }
            }
            send(JSON.stringify(results));
        }else{
            start({"code": 500});
            send("Latitude, longitude, page and radius parameters should be provided. i.e: latitude=value&longitude=value&radius=value&now=value");
        }

I have a simple byDate view for the event like so:

byDate: {
            map: function(doc){ if (doc.resource === 'Event') {emit(doc.venue.start_time, doc);}}
        }

My concern: Is there a way to sort the events first by distance within the list and then resort the sorted list by the starting time?

  • 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. Editorial Team
    Editorial Team
    2026-06-10T12:44:55+00:00Added an answer on June 10, 2026 at 12:44 pm

    If I understand you correctly, you want the closest event to come up first. If there are two events within the same distance, show the earliest first.

    This can be done by saving the calculated distance in the objects before pushing them to the result set:

    row.value._distance = d;
    results.push(row);
    

    Note that you can’t save the document now since all fields starting with an underscore _ are reserved by couchdb. But since the distance to an event is likely to be different for each request that’s fine. Just remember to remove the property if you need to save the document back to the store.

    In the next step we need to come up with a clever way of sorting your events – all the information we need is now stored in the document.

    Since JavaScript does not really like sorting complex data structures, we have to do some legwork:

    var sort = function(a,b) {
      if (JSON.stringify(a) == JSON.stringify(b)) return 0;
      return (JSON.stringify([a,b]) == JSON.stringify([a,b].sort())) ? -1 : 1
    };
    

    This function just sorts an array of simple values like so:

    > sort(["a",1], ["a", 0])
    1
    
    > sort(["a",0], ["a", 1])
    -1
    
    > sort(["a",0], ["a", 0])
    0
    

    Now for the fun part, before you send the results back to the client, you sort them:

    // ...
    results.sort(function(a, b) {
      return sort(
        [a.value._distance, a.value.venue.start_time],
        [b.value._distance, b.value.venue.start_time]
      );
    });
    
    send(JSON.stringify(results));
    

    Example:

    [{"value": {"_distance": 100, "venue": { "start_time": "Wed, 21 Mar 2012 04:31:24 -0700" } } },
     {"value": {"_distance": 212, "venue": { "start_time": "Sat, 13 Oct 2012 02:52:12 -0700" } } },
     {"value": {"_distance": 235, "venue": { "start_time": "Mon, 22 Jul 2013 12:50:20 -0700" } } },
     {"value": {"_distance": 677, "venue": { "start_time": "Thu, 09 May 2013 03:39:55 -0700" } } },
     {"value": {"_distance": 654, "venue": { "start_time": "Thu, 29 Sep 2011 15:31:46 -0700" } } },
     {"value": {"_distance": 100, "venue": { "start_time": "Tue, 20 Sep 2011 19:16:37 -0700" } } }]
    

    becomes this after using the sort function above:

    [{"value": {"_distance": 100, "venue": {"start_time": "Tue, 20 Sep 2011 19:16:37 -0700" } } },
     {"value": {"_distance": 100, "venue": {"start_time": "Wed, 21 Mar 2012 04:31:24 -0700" } } },
     {"value": {"_distance": 212, "venue": {"start_time": "Sat, 13 Oct 2012 02:52:12 -0700" } } },
     {"value": {"_distance": 235, "venue": {"start_time": "Mon, 22 Jul 2013 12:50:20 -0700" } } },
     {"value": {"_distance": 654, "venue": {"start_time": "Thu, 29 Sep 2011 15:31:46 -0700" } } },
     {"value": {"_distance": 677, "venue": {"start_time": "Thu, 09 May 2013 03:39:55 -0700" } } }]
    

    Note that after sorting, the first two objects have _distance == 100 but since the first one is earlier, it is sorted first.

    Hope that helps!

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

Sidebar

Related Questions

I have a CouchDB (1.1.1) server running that contains a lot of documents in
I have an map function in a view in CouchDB that emits non-unique two
I have a CouchDB (v0.10.0) database that is 8.2 GB in size and contains
I have some documents in CouchDB that contains a value called expire. e.g.: expire:
I have some documents in couchdb that have fields that are arrays of id's
I have a large set of documents in a CouchDB database that were just
Given that I have some number of documents in my CouchDB database, I would
I'm using iOS Couchbase Mobile to have a couchdb server on an iPad that
I have a production server running an app that uses CouchDB as its main
I am thinking about starting my first CouchDB project and coming from an ORM

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.