Search Results

Search found 16433 results on 658 pages for 'array sorting'.

Page 8/658 | < Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >

  • question about sorting

    - by skydoor
    Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is O(1) . Merge sort is always O(n log n), but its memory usage is O(n). Which algorithm we would use to implement a function that takes an array of integers and returns the max integer in the collection, assuming that the length of the array is less than 1000. What if the array length is greater than 1000?

    Read the article

  • Sorting an array in descending order in Ruby.

    - by Waseem
    Hi, I have an array of hashes like following [ { :foo => 'foo', :bar => 2 }, { :foo => 'foo', :bar => 3 }, { :foo => 'foo', :bar => 5 }, ] I am trying to sort above array in descending order according to the value of :bar in each hash. I am using sort_by like following to sort above array. a.sort_by { |h| h[:bar] } However above sorts the array in ascending order. How do I make it sort in descending order? One solution was to do following: a.sort_by { |h| -h[:bar] } But that negative sign does not seem appropriate. Any views?

    Read the article

  • Sorting a 2D numpy array by multiple axes

    - by perimosocordiae
    I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example: array([[3, 2], [6, 2], [3, 6], [3, 4], [5, 3]]) I'd like to sort it such that my points are ordered by x-coordinate, and then by y in cases where the x coordinate is the same. So the array above should look like this: array([[3, 2], [3, 4], [3, 6], [5, 3], [6, 2]]) If this was a normal Python list, I would simply define a comparator to do what I want, but as far as I can tell, numpy's sort function doesn't accept user-defined comparators. Any ideas?

    Read the article

  • Sorting an array of structs

    - by keruilin
    I have an array of structs called leaders. The struct class looks like this, for contextual info: class Leader < Struct.new(:rank, :user); end Two questions: How do I sort the array of structs by rank? How do I sort the array of structs by rank and by user.created_at?

    Read the article

  • Prevent initial array from sorting

    - by George
    I have an array where the order of the objects is important for when I finally output the array in a document. However, I'm also sorting the array in a function to find the highest value. The problem is that I after I run the function to find the highest value, I can't get the original sort order of the array back. // html document var data = [75,300,150,500,200]; createGraph(data); // js document function createGraph(data) { var maxRange = getDataRange(data); // simpleEncode() = google encoding function for graph var dataSet = simpleEncode(data,maxRange); } function getDataRange(dataArray) { var num = dataArray.sort(sortNumber); return num[0]; } I've also tried setting data to dataA and dataB and using dataB in the getDataRange function and dataA in the simpleEncode function. Either way, data always end up being sorted from highest to lowest.

    Read the article

  • Reset array keys in multidimensional array

    - by nbaumann
    I've been looking around for a solution to this with no real success. I have a multidimensional array of parents and children with no limits on depth. This is generated from a database but the issue is that the item ID becomes the key using my way of arranging a flat array into a multidimensional array like so: Array( [28] => Array ( [id] => 28 [color] => #ff24e5 [name] => Personal [parent_id] => [children] => Array ( [23] => Array ( [id] => 23 [color] => #41c3a3 [name] => Shopping [parent_id] => 28 [children] => Array ( [22] => Array ( [id] => 22 [color] => #8be32b [name] => Deals [parent_id] => 23 [children] => Array ( ) ) ) ) [150] => Array ( [id] => 150 [color] => #e9a3f0 [name] => Orders [parent_id] => 28 [children] => Array ( ) ) ) ) ) What I would like, is a function that does the following: Array ( [0] => Array ( [id] => 28 [color] => #ff24e5 [name] => Personal [parent_id] => [children] => Array ( [0] => Array ( [id] => 23 [color] => #41c3a3 [name] => Shopping [parent_id] => 28 [children] => Array ( [0] => Array ( [id] => 22 [color] => #8be32b [name] => Deals [user_id] => 1 [selected] => 0 [parent_id] => 23 [children] => Array ( ) ) ) ) [1] => Array ( [id] => 150 [color] => #e9a3f0 [name] => Orders [parent_id] => 28 [children] => Array ( ) ) ) ) ) Essentially reassign keys starting from 0. I've tried numerous methods, but I'm assuming that I need to find a recursive solution and when I tried that, it destroyed my array. I was reading up on the array_walk_recursive() function, but I don't quite know what to do beyond that. Essentially, is there a way to reset numeric keys in a multidimensional array? Thanks for the help!

    Read the article

  • How to sort an array by some specific key?

    - by vinothkumar
    I have an array look like below. $array[0]['keyword'] = 'cricket '; $array[0]['noofhits'] = '26'; $array[0]['keyword'] = 'food '; $array[0]['noofhits'] = '17'; $array[0]['keyword'] = 'mypax'; $array[0]['noofhits'] = '22'; $array[0]['keyword'] = 'next'; $array[0]['noofhits'] = '22'; $array[0]['keyword'] = 'nextbutton'; $array[0]['noofhits'] = '22'; $array[0]['keyword'] = 'picture'; $array[0]['noofhits'] = '18'; I want to sort the array using the noofhits. How can I do? Advance Thanks for your advice.

    Read the article

  • python: sorting

    - by nabizan
    hi im doing a loop so i could get dict of data, but since its a dict it's sorting alphabetical and not as i push it trought the loop ... is it possible to somehow turn off alphabetical sorting? here is how do i do that data = {} for item in container: data[item] = {} ... for key, val in item_container.iteritems(): ... data[item][key] = val whitch give me something like this data = { A : { K1 : V1, K2 : V2, K3 : V3 }, B : { K1 : V1, K2 : V2, K3 : V3 }, C : { K1 : V1, K2 : V2, K3 : V3 } } and i want it to be as i was going throught the loop, e.g. data = { B : {K2 : V2, K3 : V3, K1 : V1}, A : {K1 : V1, K2 : V2, K3 : V3}, C : {K3 : V3, K1 : V1, K2 : V2} }

    Read the article

  • How to structurally display a multi-dimensional array in PHP?

    - by Jaime Cross
    How can I display the contents of an array as follows: Company Name - Username1 - Username2 Another Company Name - Username3 The array I have created is as follows: $array[1]['company_id'] = '12'; $array[1]['company_name'] = 'ABC Company'; $array[1]['company_type'] = 'default'; $array[1]['user_id'] = '23'; $array[1]['user_name'] = 'Andrew'; $array[2]['company_id'] = '12'; $array[2]['company_name'] = 'ABC Company'; $array[2]['company_type'] = 'default'; $array[2]['user_id'] = '27'; $array[2]['user_name'] = 'Jeffrey'; $array[3]['company_id'] = '1'; $array[3]['company_name'] = 'Some Company'; $array[3]['company_type'] = 'default'; $array[3]['user_id'] = '29'; $array[3]['user_name'] = 'William'; $array[4]['company_id'] = '51'; $array[4]['company_name'] = 'My Company'; $array[4]['company_type'] = 'default'; $array[4]['user_id'] = '20'; $array[4]['user_name'] = 'Jaime';

    Read the article

  • Sorting a list of colors in one dimension?

    - by Ptah- Opener of the Mouth
    I would like to sort a one-dimensional list of colors so that colors that a typical human would perceive as "like" each other are near each other. Obviously this is a difficult or perhaps impossible problem to get "perfectly", since colors are typically described with three dimensions, but that doesn't mean that there aren't some sorting methods that look obviously more natural than others. For example, sorting by RGB doesn't work very well, as it will sort in the following order, for example: (1) R=254 G=0 B=0 (2) R=254 G=255 B=0 (3) R=255 G=0 B=0 (4) R=255 G=255 B=0 That is, it will alternate those colors red, yellow, red, yellow, with the two "reds" being essentially imperceivably different than each other, and the two yellows also being imperceivably different from each other. But sorting by HLS works much better, generally speaking, and I think HSL even better than that; with either, the reds will be next to each other, and the yellows will be next to each other. But HLS/HSL has some problems, too; things that people would perceive as "black" could be split far apart from each other, as could things that people would perceive as "white". Again, I understand that I pretty much have to accept that there will be some splits like this; I'm just wondering if anyone has found a better way than HLS/HSL. And I'm aware that "better" is somewhat arbitrary; I mean "more natural to a typical human". For example, a vague thought I've had, but have not yet tried, is perhaps "L is the most important thing if it is very high or very low", but otherwise it is the least important. Has anyone tried this? Has it worked well? What specifically did you decide "very low" and "very high" meant? And so on. Or has anyone found anything else that would improve upon HSL? I should also note that I am aware that I can define a space-filling curve through the cube of colors, and order them one-dimensionally as they would be encountered while travelling along that curve. That would eliminate perceived discontinuities. However, it's not really what I want; I want decent overall large-scale groupings more than I want perfect small-scale groupings. Thanks in advance for any help.

    Read the article

  • Dynamic Array traversal in PHP

    - by Kristoffer Bohmann
    I want to build a hierarchy from a one-dimensional array and can (almost) do so with a more or less hardcoded code. How can I make the code dynamic? Perhaps with while(isset($array[$key])) { ... }? Or, with an extra function? Like this: $out = my_extra_traverse_function($array,$key); function array_traverse($array,$key=NULL) { $out = (string) $key; $out = $array[$key] . "/" . $out; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; $key = $array[$key]; $out = $array[$key] ? $array[$key] . "/" . $out : ""; return $out; } $a = Array(102=>101, 103=>102, 105=>107, 109=>105, 111=>109, 104=>111); echo array_traverse($a,104); Output: 107/105/109/111/104

    Read the article

  • Sorting tab delimited text file based on multiple columns in natural way [duplicate]

    - by Vignesh
    This question already has an answer here: Sorting a column of CSV file resulting in 1123 appearing before 232 1 answer I am trying to sort a file based on all two columns Eg: chr19 1070019 1070020 chr16 869712 869713 chr1 1378131 1378132 chr12 189386 189387 chr4 254941 254942 chr16 1476500 1476501 chr2 1476810 1476811 chr19 313283 313284 chr17 595817 595818 chr18 656897 656898 chr19 1061829 1061830 I Tried sort -t $\t -k1,1 2,2 <filename> but doesn't work. I want the output to be sorted by first column and second column based on first column. I want to do a natural sort. Not lexical sorting. Eg: chr1 1378131 1378132 chr2 1476810 1476811 chr4 254941 254942 chr12 189386 189387 chr16 869712 869713 chr16 1476500 1476501 chr17 595817 595818 chr18 656897 656898 chr19 313283 313284 chr19 1061829 1061830 chr19 1070019 1070020 Anyone any idea?

    Read the article

  • PHP arrays - How to 1-dimensional array into nested multidimensional array?

    - by sombe
    When retrieving a hierarchical structure from MySQL (table with one ID column and one PARENT column signifying the hierarchical relationships), I map the result into an enumerated array as follows (for this example the numbers are arbitrary): Array ( [3] => Array ( [7] => Array () ), [7] => Array ( [8] => Array () ) ) Notice 3 is the parent of 7, and 7 is the parent of 8 (this could go on and on; and any parent could have multiple children). I wanted to shrink this array into a nested multidimensional array as follows: Array ( [3] => Array ( [7] => Array ( [8] => Array () ) ) ) That is, each NEW id is automatically assigned an empty array. Regardless, any ID's children will be pushed into their parent's array. Take a look at the following illustration for further clarification: This will probably result in a complicated recursive operation, since I always have to check whether a parent with any certain ID already exists (and if so, push the value into its array). Is there a built-in php function that can assist me with this? Do you have any idea as to how to go about constructing this? For what it's worth I'm using this to built a navigation bar in wordpress (which can contain categories, subcategories, posts... essentially anything).

    Read the article

  • Sorting NSTableColumn contents

    - by Yahoo
    Hey, I have a problem with sorting NSTableColumn contents. In my NSTableView there are three columns: File, Size, Path. The contents are stored in NSMutableArray. Each object in this array is a NSDictionary containing three keys: file, size and path - value for each is a NSString. In Interface Builder, in each Table Column's attributes I can choose sorting options: Selector: IB entered "compare:" which I think is ok, because I compare NSStrings. Sort Key - and that's the problem I think - I don't know what to enter here. Any clues? If you've got questions about my code, please ask.

    Read the article

  • Using a locale-dependent sorting function in Ruby/Rails

    - by knuton
    What is a good approach to sorting an array of strings in accordance with the current locale? For example the standard Array#sort puts "Ä" after "Z", which is not correct in German. I would have expected the gem I18n to offer a hook for defining my own sorting algorithms or providing collation strings or objects. In my imagination, passing this proc or string to the sort function, would make it behave as necessary. I know that this is possible in Python, for example. Google has not helped me this time. Can you? Any advice appreciated!

    Read the article

  • Casting Type array to Generic array?

    - by George R
    The short version of the question - why can't I do this? I'm restricted to .NET 3.5. T[] genericArray; // Obviously T should be float! genericArray = new T[3]{ 1.0f, 2.0f, 0.0f }; // Can't do this either, why the hell not genericArray = new float[3]{ 1.0f, 2.0f, 0.0f }; Longer version - I'm working with the Unity engine here, although that's not important. What is - I'm trying to throw conversion between its fixed Vector2 (2 floats) and Vector3 (3 floats) and my generic Vector< class. I can't cast types directly to a generic array. using UnityEngine; public struct Vector { private readonly T[] _axes; #region Constructors public Vector(int axisCount) { this._axes = new T[axisCount]; } public Vector(T x, T y) { this._axes = new T[2] { x, y }; } public Vector(T x, T y, T z) { this._axes = new T[3]{x, y, z}; } public Vector(Vector2 vector2) { // This doesn't work this._axes = new T[2] { vector2.x, vector2.y }; } public Vector(Vector3 vector3) { // Nor does this this._axes = new T[3] { vector3.x, vector3.y, vector3.z }; } #endregion #region Properties public T this[int i] { get { return _axes[i]; } set { _axes[i] = value; } } public T X { get { return _axes[0];} set { _axes[0] = value; } } public T Y { get { return _axes[1]; } set { _axes[1] = value; } } public T Z { get { return this._axes.Length (Vector2 vector2) { Vector vector = new Vector(vector2); return vector; } public static explicit operator Vector(Vector3 vector3) { Vector vector = new Vector(vector3); return vector; } #endregion }

    Read the article

  • Groovy: Sorting Columns in a view: list

    - by Luixv
    I have a Groovy application. I am rendering the view list using the following statement: render (view: 'list', model:[reportingInstanceList: reportingInstanceList, reportingInstanceTotal: i, params: params]) The list.gsp is as follows: The view is rendered but the default sorting is not working. <g:sortableColumn class="tabtitle" property="id" title="Id" titleKey="reporting.id" /> <g:sortableColumn class="tabtitle" property="company" title="Company" titleKey="reporting.company" /> Unfortunately the default sorting (by id, by company, etc) are not working. Any hint why? Thanks a lot in advance. Luis

    Read the article

  • 3-Way sorting in ultragrid

    - by M_Mogharrabi
    how can i have a ultragrid with 3-way sorting on every columns? I mean : a. Ascendiing -Indicated by default ascending SortIndicator. b. Descending- Indicated by default descending SortIndicator. c. No Sort- UnSort the column. Note: I have tried BeforeSortChanged Event but i had 2 problems: I could not get the previous column sort indicator to find out when should i disable sorting. I have got an Exception where it is saying that we can't change SortIndicator in BeforeSortChange Event

    Read the article

  • Understanding c-pointers for rows in 2-dimensional array

    - by utdiscant
    I have the following code: int main() { int n = 3, m = 4, a[n][m], i, j, (* p)[m] = a; for (i = 0; i < n; i++) for (j = 0; j < m; j++) a[i][j] = 1; p++; (*p)[2] = 9; return 0; } I have a hard time understanding what p is here, and the consequences of the operations on p in the end. Can someone give me a brief explanation of what happens. I know c-pointers in their simple settings, but here it get slightly more complicated.

    Read the article

  • Sorting NSTableColumn contents

    - by Yahoo
    Hey, I have a problem with sorting NSTableColumn contents. In my NSTableView there are three columns: File, Size, Path. The contents are stored in NSMutableArray. Each object in this array is a NSDictionary containing three keys: file, size and path - value for each is a NSString. In Interface Builder, in each Table Column's attributes I can choose sorting options: Selector: IB entered "compare:" which I think is ok, because I compare NSStrings. Sort Key - and that's the problem I think - I don't know what to enter here. Any clues? If you've got questions about my code, please ask.

    Read the article

  • Change index order in array (php)

    - by Trikks
    Hi Been kind of stuck on this one for a while now, so any help would be appreciated. I have one array (left) that contains a list of elements, the goal is to sort another arrays (right) keys with the values from the left array. The left array Array ( [0] => ID [1] => FirstName [2] => LastName [3] => Address ) The right array Array ( [0] => Array ( [FirstName] => Pim [Address] => Finland [LastName] => Svensson [ID] => 3 ) [1] => Array ( [FirstName] => Emil [Address] => Sweden [LastName] => Malm [ID] => 5 ) ) What I'm trying to accomplish would be similar to this Array ( [0] => Array ( [ID] => 3 [FirstName] => Pim [LastName] => Svensson [Address] => Finland ) Anyone? :) Oh, I'm running php 5.3, if it helps!

    Read the article

  • natural sort of text and numbers, JavaScript

    - by ptrn
    I'm looking for the easiest way to sort an array that consists of numbers and text, and a combination of these. E.g. '123asd' '19asd' '12345asd' 'asd123' 'asd12' turns into '19asd' '123asd' '12345asd' 'asd12' 'asd123' This is going to be used in combination with the solution to another question I've asked here. The sorting function in itself works, what I need is a function that can say that that '19asd' is smaller than '123asd'. I'm writing this in JavaScript. Edit: as adormitu pointed out, what I'm looking for is a function for natural sorting

    Read the article

  • How to define a static array without a contant size in a constructor of a class? (C++)

    - by Keand64
    I have a class defined as: class Obj { public: int width, height; Obj(int w, int h); } and I need it to contain a static array like so: int presc[width][height]; however, I cannot define within the class, so it it possible to create a pointer to a 2D array (and, out of curiosity, 3, 4, and 5D arrays), have that as a member of the class, and intitalize it in the constructor like: int ar[5][6]; Obj o(5, 6, &ar); If that isn't possible, is there any way to get a static array without a contant size in a class, or am I going to have to use a dynamic array? (Something I don't want to do because I don't plan on ever changing the size of the array after it's created.)

    Read the article

  • Determining the order of a list of numbers (possibly without sorting)

    - by Victor Liu
    I have an array of unique integers (e.g. val[i]), in arbitrary order, and I would like to populate another array (ord[i]) with the the sorted indexes of the integers. In other words, val[ord[i]] is in sorted order for increasing i. Right now, I just fill in ord with 0, ..., N, then sort it based on the value array, but I am wondering if we can be more efficient about it since ord is not populated to begin with. This is more of a question out of curiousity; I don't really care about the extra overhead from having to prepopulate a list and then sort it (it's small, I use insertion sort). This may be a silly question with an obvious answer, but I couldn't find anything online.

    Read the article

< Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >