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 8932967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:25:33+00:00 2026-06-15T09:25:33+00:00

So I tried implementing my Quicksort for sorting an array of Edges by their

  • 0

So I tried implementing my Quicksort for sorting an array of Edges by their weight which is in int for my Kruskal’s algorithm implementation. Is there a built function in javascript for sorting an array of objects by their properties? In this case by their weight. From smallest weight to the largest.

Here is my edge class.

    class Edge
    {            
        private int u;
        private int v;
        private int weight;

        public Edge(int i, int i2, int w)
        {
            u = i;
            v = i2;
            weight = w;
        }

        public int getU() {
            return u;
        }

        public int getV() {
            return v;
        }

        public int getWeight() {
            return weight;
        }
    }

Kruskal’s code

class MSTKruskal
    {           
        Edge[] mst(int[][] G)
        {
            Edge A[] =  new Edge[G.length - 1];               
            Forest aForest = new Forest(G.length);                
            Edge E[] = new Edge[(G.length * G.length - G.length)/2];
            int i3 = 0;
            for (int i = 0; i < G.length; i++)
            {
               for(int i2 = i+1; i2 < G.length; i2++)
               {                       
                   Edge anEdge = new Edge(i, i2, G[i][i2]);
                   E[i3] = anEdge;
                   i3++;
               }
            }
            print(E);          
            //QuickSort(E, 0, E.length);
            print(E);
            int index = 0;
            for (int i = 0; i < E.length; i++)
            {
                if (aForest.findSet(E[i].getU()) != aForest.findSet(E[i].getV()))
                {

                        A[index] = E[i];
                        index++;
                        aForest.union(E[i].getU(), E[i].getV());

                }
            }
            aForest.printA();
            return A;

        }
  • 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-15T09:25:34+00:00Added an answer on June 15, 2026 at 9:25 am

    In javascript you can pass function as parameter to sort method, this function should take 2 parameters (a and b), and return:

    • 0, if a == b
    • <0, if a < b
    • >0, if a > b

    In your case it should be something like:

    var a=[15,7,100,50];
    a.sort(function(a,b){return a.weight-b.weight;});
    

    So you can easily sort by any arrays contents’ properties or even more complex criteria.

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

Sidebar

Related Questions

I tried implementing a sorting program in mapreduce such that I have just the
I've tried implementing: http://aext.net/2009/08/perfect-sign-in-dropdown-box-likes-twitter-with-jquery/ Which works perfectly, it shows when you click the link,
As a learning experience I recently tried implementing Quicksort with 3 way partitioning in
Has anyone tried implementing Amazon's GameCircle into their Kindle Fire app? I can't get
EDIT (2011-07-23) Have gotten some very helpful answers, both of which I've tried implementing.
I just tried implementing code (in Java) for various means by which the nth
I've tried implementing the Tipsy jQuery plugin on my website which is mean't to
I tried implementing dragging on android in 2 ways: Canvas with on draw. Relative
I've tried implementing this example , but the progress never displays. My page doesn't
UPDATE: I tried implementing the method specified by Peter and am getting incorrect shadowing.

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.