Home > Net >  Can anyone help me with sort fuctions written in Javascript to C# or explain me how these lines of c
Can anyone help me with sort fuctions written in Javascript to C# or explain me how these lines of c

Time:10-04

I'm trying to transtate this Javascript function to C# method.

parametrize = (obj, join = false) ->
    arrayOfArrays = _.pairs(obj).sort()

    symbol = if join then '&' else ''

    sortedParams = ''
    _.each arrayOfArrays, (value) ->
      sortedParams  = "#{_.first(value)}=#{_.last(value)}"   symbol

    return sortedParams

This functions is written base on these scenarios.

  • parameters are sorted lexicographically by keys;
  • parameters are joined in the format key=value;
  • sig = MD5(parameters_value session_secret_key);
  • the sig value is changed to the lower case.

I would like to write this function in C# and because of lack of understanding of what javascript does. If anyone could help me explaining what Javascript functions do for example would be awesome.

Thank you :)

CodePudding user response:

So, in terms of understanding the JavaScript, the method just combines an object containing key-value pairs and combines them into a single string with either a space between or an ampersand. I'm assuming this method is used to send parameters with a get or post request, or with spaces for command line arguments.

// method that accepts an object and a join boolean (false by default)
parametrize = (obj, join = false) ->

// sort the obects and store as an array
arrayOfArrays = _.pairs(obj).sort()

// decide which join character to use
symbol = if join then '&' else ''

// Create the variable to return
sortedParams = ''

// loop through each of the array rows 
_.each arrayOfArrays, (value) ->
  // add each row into the string and add the joining symbol
  sortedParams  = "#{_.first(value)}=#{_.last(value)}"   symbol

// Once complete, return the created string
return sortedParams

As for the c# side of things, this is all fairly rudimentary stuff. A quick google on loops, strings (including concatenation) and methods will sort you out.

  • Related